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( ...@@ -4194,17 +4194,20 @@ typename ParserBase<Impl>::ExpressionT ParserBase<Impl>::ParseClassLiteral(
prop_info.is_computed_name) { prop_info.is_computed_name) {
class_info.has_static_computed_names = true; class_info.has_static_computed_names = true;
} }
if (prop_info.is_computed_name &&
property_kind == ClassLiteralProperty::FIELD) {
DCHECK(!prop_info.is_private);
class_info.computed_field_count++;
}
is_constructor &= class_info.has_seen_constructor; is_constructor &= class_info.has_seen_constructor;
impl()->DeclareClassProperty(name, property, prop_info.name, property_kind, if (V8_UNLIKELY(property_kind == ClassLiteralProperty::FIELD)) {
prop_info.is_static, is_constructor, if (prop_info.is_computed_name) {
prop_info.is_computed_name, DCHECK(!prop_info.is_private);
prop_info.is_private, &class_info); class_info.computed_field_count++;
}
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(); impl()->InferFunctionName();
} }
......
...@@ -3041,45 +3041,20 @@ Variable* Parser::CreateSyntheticContextVariable(const AstRawString* name) { ...@@ -3041,45 +3041,20 @@ Variable* Parser::CreateSyntheticContextVariable(const AstRawString* name) {
return proxy->var(); return proxy->var();
} }
// This method declares a property of the given class. It updates the void Parser::DeclareClassField(ClassLiteralProperty* property,
// following fields of class_info, as appropriate: const AstRawString* property_name,
// - constructor bool is_static, bool is_computed_name,
// - properties bool is_private, ClassInfo* class_info) {
void Parser::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) {
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;
}
DCHECK(allow_harmony_public_fields() || allow_harmony_private_fields()); DCHECK(allow_harmony_public_fields() || allow_harmony_private_fields());
if (is_static) { if (is_static) {
DCHECK(allow_harmony_static_fields());
DCHECK_EQ(kind, ClassLiteralProperty::FIELD);
class_info->static_fields->Add(property, zone()); class_info->static_fields->Add(property, zone());
} else { } else {
class_info->instance_fields->Add(property, zone()); class_info->instance_fields->Add(property, zone());
} }
DCHECK_IMPLIES(is_computed_name, !is_private);
if (is_computed_name) { if (is_computed_name) {
DCHECK_EQ(kind, ClassLiteralProperty::FIELD);
DCHECK(!is_private);
// We create a synthetic variable name here so that scope // We create a synthetic variable name here so that scope
// analysis doesn't dedupe the vars. // analysis doesn't dedupe the vars.
Variable* computed_name_var = Variable* computed_name_var =
...@@ -3087,15 +3062,33 @@ void Parser::DeclareClassProperty(const AstRawString* class_name, ...@@ -3087,15 +3062,33 @@ void Parser::DeclareClassProperty(const AstRawString* class_name,
ast_value_factory(), class_info->computed_field_count)); ast_value_factory(), class_info->computed_field_count));
property->set_computed_name_var(computed_name_var); property->set_computed_name_var(computed_name_var);
class_info->properties->Add(property, zone()); class_info->properties->Add(property, zone());
} } else if (is_private) {
if (kind == ClassLiteralProperty::FIELD && is_private) {
Variable* private_name_var = CreateSyntheticContextVariable(property_name); Variable* private_name_var = CreateSyntheticContextVariable(property_name);
property->set_private_name_var(private_name_var); property->set_private_name_var(private_name_var);
class_info->properties->Add(property, zone()); 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( FunctionLiteral* Parser::CreateInitializerFunction(
const char* name, DeclarationScope* scope, const char* name, DeclarationScope* scope,
ZonePtrList<ClassLiteral::Property>* fields) { ZonePtrList<ClassLiteral::Property>* fields) {
......
...@@ -354,11 +354,12 @@ class V8_EXPORT_PRIVATE Parser : public NON_EXPORTED_BASE(ParserBase<Parser>) { ...@@ -354,11 +354,12 @@ class V8_EXPORT_PRIVATE Parser : public NON_EXPORTED_BASE(ParserBase<Parser>) {
void DeclareClassVariable(const AstRawString* name, ClassInfo* class_info, void DeclareClassVariable(const AstRawString* name, ClassInfo* class_info,
int class_token_pos); int class_token_pos);
void DeclareClassProperty(const AstRawString* class_name, void DeclareClassProperty(const AstRawString* class_name,
ClassLiteralProperty* property, ClassLiteralProperty* property, bool is_constructor,
const AstRawString* property_name, ClassInfo* class_info);
ClassLiteralProperty::Kind kind, bool is_static, void DeclareClassField(ClassLiteralProperty* property,
bool is_constructor, bool is_computed_name, const AstRawString* property_name, bool is_static,
bool is_private, ClassInfo* class_info); bool is_computed_name, bool is_private,
ClassInfo* class_info);
Expression* RewriteClassLiteral(Scope* block_scope, const AstRawString* name, Expression* RewriteClassLiteral(Scope* block_scope, const AstRawString* name,
ClassInfo* class_info, int pos, int end_pos); ClassInfo* class_info, int pos, int end_pos);
Statement* DeclareNative(const AstRawString* name, int pos); Statement* DeclareNative(const AstRawString* name, int pos);
......
...@@ -1252,21 +1252,20 @@ class PreParser : public ParserBase<PreParser> { ...@@ -1252,21 +1252,20 @@ class PreParser : public ParserBase<PreParser> {
} }
V8_INLINE void DeclareClassProperty(const PreParserIdentifier& class_name, V8_INLINE void DeclareClassProperty(const PreParserIdentifier& class_name,
const PreParserExpression& property, const PreParserExpression& property,
const PreParserIdentifier& property_name, bool is_constructor,
ClassLiteralProperty::Kind kind, ClassInfo* class_info) {}
bool is_static, bool is_constructor,
bool is_computed_name, bool is_private, V8_INLINE void DeclareClassField(const PreParserExpression& property,
ClassInfo* class_info) { const PreParserIdentifier& property_name,
if (kind == ClassLiteralProperty::FIELD && !is_private && bool is_static, bool is_computed_name,
is_computed_name) { bool is_private, ClassInfo* class_info) {
DCHECK_IMPLIES(is_computed_name, !is_private);
if (is_computed_name) {
scope()->DeclareVariableName( scope()->DeclareVariableName(
ClassFieldVariableName(ast_value_factory(), ClassFieldVariableName(ast_value_factory(),
class_info->computed_field_count), class_info->computed_field_count),
VariableMode::kConst); VariableMode::kConst);
} } else if (is_private && property_name.string_ != nullptr) {
if (kind == ClassLiteralProperty::FIELD && is_private &&
property_name.string_ != nullptr) {
scope()->DeclareVariableName(property_name.string_, VariableMode::kConst); 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