Commit 412d56f8 authored by Seth Brenith's avatar Seth Brenith Committed by Commit Bot

[torque] Require "extends" clause for class declarations

Currently it's possible to hit an internal compiler error by declaring a
non-extern class that doesn't extend anything. It's not very meanigful
for a class to not extend from anything, so the parser should enforce
this requirement.

Change-Id: I38064f87345d28ce84521261bbfd33d9b1c71334
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2153847
Commit-Queue: Seth Brenith <seth.brenith@microsoft.com>
Reviewed-by: 's avatarTobias Tebbi <tebbi@chromium.org>
Cr-Commit-Position: refs/heads/master@{#67212}
parent 1568a47c
...@@ -1175,8 +1175,7 @@ struct ClassBody : AstNode { ...@@ -1175,8 +1175,7 @@ struct ClassBody : AstNode {
struct ClassDeclaration : TypeDeclaration { struct ClassDeclaration : TypeDeclaration {
DEFINE_AST_NODE_LEAF_BOILERPLATE(ClassDeclaration) DEFINE_AST_NODE_LEAF_BOILERPLATE(ClassDeclaration)
ClassDeclaration(SourcePosition pos, Identifier* name, ClassFlags flags, ClassDeclaration(SourcePosition pos, Identifier* name, ClassFlags flags,
base::Optional<TypeExpression*> super, TypeExpression* super, base::Optional<std::string> generates,
base::Optional<std::string> generates,
std::vector<Declaration*> methods, std::vector<Declaration*> methods,
std::vector<ClassFieldExpression> fields, std::vector<ClassFieldExpression> fields,
InstanceTypeConstraints instance_type_constraints) InstanceTypeConstraints instance_type_constraints)
...@@ -1188,7 +1187,7 @@ struct ClassDeclaration : TypeDeclaration { ...@@ -1188,7 +1187,7 @@ struct ClassDeclaration : TypeDeclaration {
fields(std::move(fields)), fields(std::move(fields)),
instance_type_constraints(std::move(instance_type_constraints)) {} instance_type_constraints(std::move(instance_type_constraints)) {}
ClassFlags flags; ClassFlags flags;
base::Optional<TypeExpression*> super; TypeExpression* super;
base::Optional<std::string> generates; base::Optional<std::string> generates;
std::vector<Declaration*> methods; std::vector<Declaration*> methods;
std::vector<ClassFieldExpression> fields; std::vector<ClassFieldExpression> fields;
......
...@@ -906,8 +906,8 @@ base::Optional<ParseResult> MakeClassDeclaration( ...@@ -906,8 +906,8 @@ base::Optional<ParseResult> MakeClassDeclaration(
if (!IsValidTypeName(name->value)) { if (!IsValidTypeName(name->value)) {
NamingConventionError("Type", name, "UpperCamelCase"); NamingConventionError("Type", name, "UpperCamelCase");
} }
auto extends = child_results->NextAs<base::Optional<TypeExpression*>>(); auto extends = child_results->NextAs<TypeExpression*>();
if (extends && !BasicTypeExpression::DynamicCast(*extends)) { if (!BasicTypeExpression::DynamicCast(extends)) {
ReportError("Expected type name in extends clause."); ReportError("Expected type name in extends clause.");
} }
auto generates = child_results->NextAs<base::Optional<std::string>>(); auto generates = child_results->NextAs<base::Optional<std::string>>();
...@@ -2358,8 +2358,7 @@ struct TorqueGrammar : Grammar { ...@@ -2358,8 +2358,7 @@ struct TorqueGrammar : Grammar {
&externalString, Token(";")}, &externalString, Token(";")},
AsSingletonVector<Declaration*, MakeExternConstDeclaration>()), AsSingletonVector<Declaration*, MakeExternConstDeclaration>()),
Rule({annotations, CheckIf(Token("extern")), CheckIf(Token("transient")), Rule({annotations, CheckIf(Token("extern")), CheckIf(Token("transient")),
OneOf({"class", "shape"}), &name, OneOf({"class", "shape"}), &name, Token("extends"), &type,
Optional<TypeExpression*>(Sequence({Token("extends"), &type})),
Optional<std::string>( Optional<std::string>(
Sequence({Token("generates"), &externalString})), Sequence({Token("generates"), &externalString})),
&optionalClassBody}, &optionalClassBody},
......
...@@ -247,7 +247,7 @@ const ClassType* TypeVisitor::ComputeType( ...@@ -247,7 +247,7 @@ const ClassType* TypeVisitor::ComputeType(
ClassFlags flags = decl->flags; ClassFlags flags = decl->flags;
bool is_shape = flags & ClassFlag::kIsShape; bool is_shape = flags & ClassFlag::kIsShape;
std::string generates = decl->name->value; std::string generates = decl->name->value;
const Type* super_type = TypeVisitor::ComputeType(*decl->super); const Type* super_type = TypeVisitor::ComputeType(decl->super);
if (is_shape) { if (is_shape) {
if (!(flags & ClassFlag::kExtern)) { if (!(flags & ClassFlag::kExtern)) {
ReportError("Shapes must be extern, add \"extern\" to the declaration."); ReportError("Shapes must be extern, add \"extern\" to the declaration.");
...@@ -266,9 +266,6 @@ const ClassType* TypeVisitor::ComputeType( ...@@ -266,9 +266,6 @@ const ClassType* TypeVisitor::ComputeType(
// support for type-checks on the C++ side. // support for type-checks on the C++ side.
generates = super_class->name(); generates = super_class->name();
} }
if (!decl->super) {
ReportError("Extern class must extend another type.");
}
if (super_type != TypeOracle::GetStrongTaggedType()) { if (super_type != TypeOracle::GetStrongTaggedType()) {
const ClassType* super_class = ClassType::DynamicCast(super_type); const ClassType* super_class = ClassType::DynamicCast(super_type);
if (!super_class) { if (!super_class) {
......
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