Commit 64c85cf4 authored by Sathya Gunasekaran's avatar Sathya Gunasekaran Committed by Commit Bot

[parser] Refactor class field declaration


Change-Id: Ieed2a202cbbceaad8a598d359fcbd02944edfdb4
Reviewed-on: https://chromium-review.googlesource.com/c/1398685
Commit-Queue: Sathya Gunasekaran <gsathya@chromium.org>
Reviewed-by: 's avatarAdam Klein <adamk@chromium.org>
Cr-Commit-Position: refs/heads/master@{#58605}
parent 75eed9af
......@@ -4194,17 +4194,20 @@ typename ParserBase<Impl>::ExpressionT ParserBase<Impl>::ParseClassLiteral(
prop_info.is_computed_name) {
class_info.has_static_computed_names = true;
}
if (prop_info.is_computed_name &&
property_kind == ClassLiteralProperty::FIELD) {
is_constructor &= class_info.has_seen_constructor;
if (V8_UNLIKELY(property_kind == ClassLiteralProperty::FIELD)) {
if (prop_info.is_computed_name) {
DCHECK(!prop_info.is_private);
class_info.computed_field_count++;
}
is_constructor &= class_info.has_seen_constructor;
impl()->DeclareClassProperty(name, property, prop_info.name, property_kind,
prop_info.is_static, is_constructor,
impl()->DeclareClassField(property, prop_info.name, prop_info.is_static,
prop_info.is_computed_name,
prop_info.is_private, &class_info);
} else {
impl()->DeclareClassProperty(name, property, is_constructor, &class_info);
}
impl()->InferFunctionName();
}
......
......@@ -3041,45 +3041,20 @@ Variable* Parser::CreateSyntheticContextVariable(const AstRawString* name) {
return proxy->var();
}
// This method declares a property of the given class. It updates the
// following fields of class_info, as appropriate:
// - constructor
// - properties
void Parser::DeclareClassProperty(const AstRawString* class_name,
ClassLiteralProperty* property,
void Parser::DeclareClassField(ClassLiteralProperty* property,
const AstRawString* property_name,
ClassLiteralProperty::Kind kind,
bool is_static, bool is_constructor,
bool is_computed_name, bool is_private,
ClassInfo* class_info) {
if (is_constructor) {
DCHECK(!class_info->constructor);
class_info->constructor = property->value()->AsFunctionLiteral();
DCHECK_NOT_NULL(class_info->constructor);
class_info->constructor->set_raw_name(
class_name != nullptr ? ast_value_factory()->NewConsString(class_name)
: nullptr);
return;
}
if (kind != ClassLiteralProperty::FIELD) {
class_info->properties->Add(property, zone());
return;
}
bool is_static, bool is_computed_name,
bool is_private, ClassInfo* class_info) {
DCHECK(allow_harmony_public_fields() || allow_harmony_private_fields());
if (is_static) {
DCHECK(allow_harmony_static_fields());
DCHECK_EQ(kind, ClassLiteralProperty::FIELD);
class_info->static_fields->Add(property, zone());
} else {
class_info->instance_fields->Add(property, zone());
}
DCHECK_IMPLIES(is_computed_name, !is_private);
if (is_computed_name) {
DCHECK_EQ(kind, ClassLiteralProperty::FIELD);
DCHECK(!is_private);
// We create a synthetic variable name here so that scope
// analysis doesn't dedupe the vars.
Variable* computed_name_var =
......@@ -3087,15 +3062,33 @@ void Parser::DeclareClassProperty(const AstRawString* class_name,
ast_value_factory(), class_info->computed_field_count));
property->set_computed_name_var(computed_name_var);
class_info->properties->Add(property, zone());
}
if (kind == ClassLiteralProperty::FIELD && is_private) {
} else if (is_private) {
Variable* private_name_var = CreateSyntheticContextVariable(property_name);
property->set_private_name_var(private_name_var);
class_info->properties->Add(property, zone());
}
}
// This method declares a property of the given class. It updates the
// following fields of class_info, as appropriate:
// - constructor
// - properties
void Parser::DeclareClassProperty(const AstRawString* class_name,
ClassLiteralProperty* property,
bool is_constructor, ClassInfo* class_info) {
if (is_constructor) {
DCHECK(!class_info->constructor);
class_info->constructor = property->value()->AsFunctionLiteral();
DCHECK_NOT_NULL(class_info->constructor);
class_info->constructor->set_raw_name(
class_name != nullptr ? ast_value_factory()->NewConsString(class_name)
: nullptr);
return;
}
class_info->properties->Add(property, zone());
}
FunctionLiteral* Parser::CreateInitializerFunction(
const char* name, DeclarationScope* scope,
ZonePtrList<ClassLiteral::Property>* fields) {
......
......@@ -354,11 +354,12 @@ class V8_EXPORT_PRIVATE Parser : public NON_EXPORTED_BASE(ParserBase<Parser>) {
void DeclareClassVariable(const AstRawString* name, ClassInfo* class_info,
int class_token_pos);
void DeclareClassProperty(const AstRawString* class_name,
ClassLiteralProperty* property,
const AstRawString* property_name,
ClassLiteralProperty::Kind kind, bool is_static,
bool is_constructor, bool is_computed_name,
bool is_private, ClassInfo* class_info);
ClassLiteralProperty* property, bool is_constructor,
ClassInfo* class_info);
void DeclareClassField(ClassLiteralProperty* property,
const AstRawString* property_name, bool is_static,
bool is_computed_name, bool is_private,
ClassInfo* class_info);
Expression* RewriteClassLiteral(Scope* block_scope, const AstRawString* name,
ClassInfo* class_info, int pos, int end_pos);
Statement* DeclareNative(const AstRawString* name, int pos);
......
......@@ -1252,21 +1252,20 @@ class PreParser : public ParserBase<PreParser> {
}
V8_INLINE void DeclareClassProperty(const PreParserIdentifier& class_name,
const PreParserExpression& property,
bool is_constructor,
ClassInfo* class_info) {}
V8_INLINE void DeclareClassField(const PreParserExpression& property,
const PreParserIdentifier& property_name,
ClassLiteralProperty::Kind kind,
bool is_static, bool is_constructor,
bool is_computed_name, bool is_private,
ClassInfo* class_info) {
if (kind == ClassLiteralProperty::FIELD && !is_private &&
is_computed_name) {
bool is_static, bool is_computed_name,
bool is_private, ClassInfo* class_info) {
DCHECK_IMPLIES(is_computed_name, !is_private);
if (is_computed_name) {
scope()->DeclareVariableName(
ClassFieldVariableName(ast_value_factory(),
class_info->computed_field_count),
VariableMode::kConst);
}
if (kind == ClassLiteralProperty::FIELD && is_private &&
property_name.string_ != nullptr) {
} else if (is_private && property_name.string_ != nullptr) {
scope()->DeclareVariableName(property_name.string_, VariableMode::kConst);
}
}
......
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