From 412d56f81ae4396bdf0b39aec69cc4da1f7d7984 Mon Sep 17 00:00:00 2001 From: Seth Brenith Date: Fri, 17 Apr 2020 09:36:36 -0700 Subject: [PATCH] [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 Reviewed-by: Tobias Tebbi Cr-Commit-Position: refs/heads/master@{#67212} --- src/torque/ast.h | 5 ++--- src/torque/torque-parser.cc | 7 +++---- src/torque/type-visitor.cc | 5 +---- 3 files changed, 6 insertions(+), 11 deletions(-) diff --git a/src/torque/ast.h b/src/torque/ast.h index 93e0622aee..d05338923f 100644 --- a/src/torque/ast.h +++ b/src/torque/ast.h @@ -1175,8 +1175,7 @@ struct ClassBody : AstNode { struct ClassDeclaration : TypeDeclaration { DEFINE_AST_NODE_LEAF_BOILERPLATE(ClassDeclaration) ClassDeclaration(SourcePosition pos, Identifier* name, ClassFlags flags, - base::Optional super, - base::Optional generates, + TypeExpression* super, base::Optional generates, std::vector methods, std::vector fields, InstanceTypeConstraints instance_type_constraints) @@ -1188,7 +1187,7 @@ struct ClassDeclaration : TypeDeclaration { fields(std::move(fields)), instance_type_constraints(std::move(instance_type_constraints)) {} ClassFlags flags; - base::Optional super; + TypeExpression* super; base::Optional generates; std::vector methods; std::vector fields; diff --git a/src/torque/torque-parser.cc b/src/torque/torque-parser.cc index 1713af0b70..aced93e79d 100644 --- a/src/torque/torque-parser.cc +++ b/src/torque/torque-parser.cc @@ -906,8 +906,8 @@ base::Optional MakeClassDeclaration( if (!IsValidTypeName(name->value)) { NamingConventionError("Type", name, "UpperCamelCase"); } - auto extends = child_results->NextAs>(); - if (extends && !BasicTypeExpression::DynamicCast(*extends)) { + auto extends = child_results->NextAs(); + if (!BasicTypeExpression::DynamicCast(extends)) { ReportError("Expected type name in extends clause."); } auto generates = child_results->NextAs>(); @@ -2358,8 +2358,7 @@ struct TorqueGrammar : Grammar { &externalString, Token(";")}, AsSingletonVector()), Rule({annotations, CheckIf(Token("extern")), CheckIf(Token("transient")), - OneOf({"class", "shape"}), &name, - Optional(Sequence({Token("extends"), &type})), + OneOf({"class", "shape"}), &name, Token("extends"), &type, Optional( Sequence({Token("generates"), &externalString})), &optionalClassBody}, diff --git a/src/torque/type-visitor.cc b/src/torque/type-visitor.cc index 10ed87d247..fc1adb19f6 100644 --- a/src/torque/type-visitor.cc +++ b/src/torque/type-visitor.cc @@ -247,7 +247,7 @@ const ClassType* TypeVisitor::ComputeType( ClassFlags flags = decl->flags; bool is_shape = flags & ClassFlag::kIsShape; 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 (!(flags & ClassFlag::kExtern)) { ReportError("Shapes must be extern, add \"extern\" to the declaration."); @@ -266,9 +266,6 @@ const ClassType* TypeVisitor::ComputeType( // support for type-checks on the C++ side. generates = super_class->name(); } - if (!decl->super) { - ReportError("Extern class must extend another type."); - } if (super_type != TypeOracle::GetStrongTaggedType()) { const ClassType* super_class = ClassType::DynamicCast(super_type); if (!super_class) {