nicer error messages when required 'generates' clause is missing

Currently, it is possible to make the Torque compiler emit uncompilable
C++ code if a 'generates' clause is missing. Rather than making the
developer spelunk through generated code to find out what went wrong,
we can catch this error earlier and print a useful message.

Bug: v8:7793
Change-Id: I49fabc1d39f398bf322523901941494b8dab0506
Reviewed-on: https://chromium-review.googlesource.com/c/1477964
Reviewed-by: Tobias Tebbi <tebbi@chromium.org>
Commit-Queue: Seth Brenith <seth.brenith@microsoft.com>
Cr-Commit-Position: refs/heads/master@{#59744}
This commit is contained in:
Seth Brenith 2019-02-20 08:18:37 -08:00 committed by Commit Bot
parent 3d943e7791
commit 0b07a88815
2 changed files with 44 additions and 24 deletions

View File

@ -75,7 +75,25 @@ bool Type::IsAbstractName(const std::string& name) const {
return AbstractType::cast(this)->name() == name;
}
std::string AbstractType::GetGeneratedTNodeTypeName() const {
std::string Type::GetGeneratedTypeName() const {
std::string result = GetGeneratedTypeNameImpl();
if (result.empty() || result == "compiler::TNode<>") {
ReportError("Generated type is required for type '", ToString(),
"'. Use 'generates' clause in definition.");
}
return result;
}
std::string Type::GetGeneratedTNodeTypeName() const {
std::string result = GetGeneratedTNodeTypeNameImpl();
if (result.empty()) {
ReportError("Generated TNode type is required for type '", ToString(),
"'. Use 'generates' clause in definition.");
}
return result;
}
std::string AbstractType::GetGeneratedTNodeTypeNameImpl() const {
return generated_type_;
}
@ -124,7 +142,7 @@ std::string UnionType::MangledName() const {
return result.str();
}
std::string UnionType::GetGeneratedTNodeTypeName() const {
std::string UnionType::GetGeneratedTNodeTypeNameImpl() const {
if (types_.size() <= 3) {
std::set<std::string> members;
for (const Type* t : types_) {
@ -250,7 +268,7 @@ const Field& AggregateType::LookupField(const std::string& name) const {
ReportError("no field ", name, " found");
}
std::string StructType::GetGeneratedTypeName() const {
std::string StructType::GetGeneratedTypeNameImpl() const {
return nspace()->ExternalName() + "::" + name();
}
@ -300,7 +318,7 @@ bool ClassType::HasIndexedField() const {
return false;
}
std::string ClassType::GetGeneratedTNodeTypeName() const {
std::string ClassType::GetGeneratedTNodeTypeNameImpl() const {
if (!IsExtern()) return generates_;
std::string prefix = nspace()->IsDefaultNamespace()
? std::string{}
@ -308,7 +326,7 @@ std::string ClassType::GetGeneratedTNodeTypeName() const {
return prefix + generates_;
}
std::string ClassType::GetGeneratedTypeName() const {
std::string ClassType::GetGeneratedTypeNameImpl() const {
return IsConstexpr() ? GetGeneratedTNodeTypeName()
: "compiler::TNode<" + GetGeneratedTNodeTypeName() + ">";
}

View File

@ -120,8 +120,8 @@ class Type : public TypeBase {
return IsAbstractName(CONSTEXPR_BOOL_TYPE_STRING);
}
bool IsVoidOrNever() const { return IsVoid() || IsNever(); }
virtual std::string GetGeneratedTypeName() const = 0;
virtual std::string GetGeneratedTNodeTypeName() const = 0;
std::string GetGeneratedTypeName() const;
std::string GetGeneratedTNodeTypeName() const;
virtual bool IsConstexpr() const = 0;
virtual bool IsTransient() const { return false; }
virtual const Type* NonConstexprVersion() const = 0;
@ -135,6 +135,8 @@ class Type : public TypeBase {
void set_parent(const Type* t) { parent_ = t; }
int Depth() const;
virtual std::string ToExplicitString() const = 0;
virtual std::string GetGeneratedTypeNameImpl() const = 0;
virtual std::string GetGeneratedTNodeTypeNameImpl() const = 0;
private:
bool IsAbstractName(const std::string& name) const;
@ -180,14 +182,14 @@ std::ostream& operator<<(std::ostream& os, const Field& name_and_type);
class TopType final : public Type {
public:
DECLARE_TYPE_BOILERPLATE(TopType)
virtual std::string MangledName() const { return "top"; }
virtual std::string GetGeneratedTypeName() const { UNREACHABLE(); }
virtual std::string GetGeneratedTNodeTypeName() const {
std::string MangledName() const override { return "top"; }
std::string GetGeneratedTypeNameImpl() const override { UNREACHABLE(); }
std::string GetGeneratedTNodeTypeNameImpl() const override {
return source_type_->GetGeneratedTNodeTypeName();
}
virtual bool IsConstexpr() const { return false; }
virtual const Type* NonConstexprVersion() const { return nullptr; }
virtual std::string ToExplicitString() const {
bool IsConstexpr() const override { return false; }
const Type* NonConstexprVersion() const override { return nullptr; }
std::string ToExplicitString() const override {
std::stringstream s;
s << "inaccessible " + source_type_->ToString();
return s.str();
@ -216,11 +218,11 @@ class AbstractType final : public Type {
std::replace(str.begin(), str.end(), ' ', '_');
return "AT" + str;
}
std::string GetGeneratedTypeName() const override {
std::string GetGeneratedTypeNameImpl() const override {
return IsConstexpr() ? generated_type_
: "compiler::TNode<" + generated_type_ + ">";
}
std::string GetGeneratedTNodeTypeName() const override;
std::string GetGeneratedTNodeTypeNameImpl() const override;
bool IsConstexpr() const override {
return name().substr(0, strlen(CONSTEXPR_TYPE_PREFIX)) ==
CONSTEXPR_TYPE_PREFIX;
@ -258,10 +260,10 @@ class BuiltinPointerType final : public Type {
DECLARE_TYPE_BOILERPLATE(BuiltinPointerType)
std::string ToExplicitString() const override;
std::string MangledName() const override;
std::string GetGeneratedTypeName() const override {
std::string GetGeneratedTypeNameImpl() const override {
return parent()->GetGeneratedTypeName();
}
std::string GetGeneratedTNodeTypeName() const override {
std::string GetGeneratedTNodeTypeNameImpl() const override {
return parent()->GetGeneratedTNodeTypeName();
}
bool IsConstexpr() const override {
@ -312,10 +314,10 @@ class UnionType final : public Type {
DECLARE_TYPE_BOILERPLATE(UnionType)
std::string ToExplicitString() const override;
std::string MangledName() const override;
std::string GetGeneratedTypeName() const override {
std::string GetGeneratedTypeNameImpl() const override {
return "compiler::TNode<" + GetGeneratedTNodeTypeName() + ">";
}
std::string GetGeneratedTNodeTypeName() const override;
std::string GetGeneratedTNodeTypeNameImpl() const override;
bool IsConstexpr() const override {
DCHECK_EQ(false, parent()->IsConstexpr());
@ -401,8 +403,8 @@ class AggregateType : public Type {
public:
DECLARE_TYPE_BOILERPLATE(AggregateType)
std::string MangledName() const override { return name_; }
std::string GetGeneratedTypeName() const override { UNREACHABLE(); }
std::string GetGeneratedTNodeTypeName() const override { UNREACHABLE(); }
std::string GetGeneratedTypeNameImpl() const override { UNREACHABLE(); }
std::string GetGeneratedTNodeTypeNameImpl() const override { UNREACHABLE(); }
const Type* NonConstexprVersion() const override { return this; }
bool IsConstexpr() const override { return false; }
@ -449,7 +451,7 @@ class StructType final : public AggregateType {
public:
DECLARE_TYPE_BOILERPLATE(StructType)
std::string ToExplicitString() const override;
std::string GetGeneratedTypeName() const override;
std::string GetGeneratedTypeNameImpl() const override;
void SetDerivedFrom(const ClassType* derived_from) {
derived_from_ = derived_from;
@ -474,8 +476,8 @@ class ClassType final : public AggregateType {
public:
DECLARE_TYPE_BOILERPLATE(ClassType)
std::string ToExplicitString() const override;
std::string GetGeneratedTypeName() const override;
std::string GetGeneratedTNodeTypeName() const override;
std::string GetGeneratedTypeNameImpl() const override;
std::string GetGeneratedTNodeTypeNameImpl() const override;
bool IsExtern() const { return is_extern_; }
bool IsTransient() const override { return transient_; }
bool HasIndexedField() const override;