ast: Have all AST types derive from ast::Node

A common base class is required to move from std::unique_ptr<> to raw pointers for AST types.

Also unifies a bunch of similar APIs.

Bug: tint:322
Change-Id: If829f8c3f22069adf62751365f1f1eeb646aba08
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/32660
Commit-Queue: Ben Clayton <bclayton@google.com>
Commit-Queue: dan sinclair <dsinclair@chromium.org>
Reviewed-by: dan sinclair <dsinclair@chromium.org>
This commit is contained in:
Ben Clayton 2020-11-13 21:43:58 +00:00 committed by Commit Bot service account
parent 196e097730
commit 6f58546193
44 changed files with 104 additions and 79 deletions

View File

@ -40,9 +40,6 @@ class ArrayDecoration : public Decoration {
/// @returns the decoration as a stride decoration /// @returns the decoration as a stride decoration
StrideDecoration* AsStride(); StrideDecoration* AsStride();
/// @returns the decoration as a string
virtual std::string to_str() const = 0;
protected: protected:
/// Constructor /// Constructor
/// @param source the source of this decoration /// @param source the source of this decoration

View File

@ -26,7 +26,8 @@ bool BindingDecoration::IsBinding() const {
return true; return true;
} }
void BindingDecoration::to_str(std::ostream& out) const { void BindingDecoration::to_str(std::ostream& out, size_t indent) const {
make_indent(out, indent);
out << "BindingDecoration{" << value_ << "}" << std::endl; out << "BindingDecoration{" << value_ << "}" << std::endl;
} }

View File

@ -38,8 +38,9 @@ class BindingDecoration : public VariableDecoration {
uint32_t value() const { return value_; } uint32_t value() const { return value_; }
/// Outputs the decoration to the given stream /// Outputs the decoration to the given stream
/// @param out the stream to output too /// @param out the stream to write to
void to_str(std::ostream& out) const override; /// @param indent number of spaces to indent the node when writing
void to_str(std::ostream& out, size_t indent) const override;
private: private:
uint32_t value_; uint32_t value_;

View File

@ -39,7 +39,7 @@ TEST_F(BindingDecorationTest, Is) {
TEST_F(BindingDecorationTest, ToStr) { TEST_F(BindingDecorationTest, ToStr) {
BindingDecoration d{2, Source{}}; BindingDecoration d{2, Source{}};
std::ostringstream out; std::ostringstream out;
d.to_str(out); d.to_str(out, 0);
EXPECT_EQ(out.str(), R"(BindingDecoration{2} EXPECT_EQ(out.str(), R"(BindingDecoration{2}
)"); )");
} }

View File

@ -26,7 +26,8 @@ bool BuiltinDecoration::IsBuiltin() const {
return true; return true;
} }
void BuiltinDecoration::to_str(std::ostream& out) const { void BuiltinDecoration::to_str(std::ostream& out, size_t indent) const {
make_indent(out, indent);
out << "BuiltinDecoration{" << builtin_ << "}" << std::endl; out << "BuiltinDecoration{" << builtin_ << "}" << std::endl;
} }

View File

@ -37,8 +37,9 @@ class BuiltinDecoration : public VariableDecoration {
Builtin value() const { return builtin_; } Builtin value() const { return builtin_; }
/// Outputs the decoration to the given stream /// Outputs the decoration to the given stream
/// @param out the stream to output too /// @param out the stream to write to
void to_str(std::ostream& out) const override; /// @param indent number of spaces to indent the node when writing
void to_str(std::ostream& out, size_t indent) const override;
private: private:
Builtin builtin_ = Builtin::kNone; Builtin builtin_ = Builtin::kNone;

View File

@ -39,7 +39,7 @@ TEST_F(BuiltinDecorationTest, Is) {
TEST_F(BuiltinDecorationTest, ToStr) { TEST_F(BuiltinDecorationTest, ToStr) {
BuiltinDecoration d{Builtin::kFragDepth, Source{}}; BuiltinDecoration d{Builtin::kFragDepth, Source{}};
std::ostringstream out; std::ostringstream out;
d.to_str(out); d.to_str(out, 0);
EXPECT_EQ(out.str(), R"(BuiltinDecoration{frag_depth} EXPECT_EQ(out.str(), R"(BuiltinDecoration{frag_depth}
)"); )");
} }

View File

@ -26,7 +26,8 @@ bool ConstantIdDecoration::IsConstantId() const {
return true; return true;
} }
void ConstantIdDecoration::to_str(std::ostream& out) const { void ConstantIdDecoration::to_str(std::ostream& out, size_t indent) const {
make_indent(out, indent);
out << "ConstantIdDecoration{" << value_ << "}" << std::endl; out << "ConstantIdDecoration{" << value_ << "}" << std::endl;
} }

View File

@ -37,8 +37,9 @@ class ConstantIdDecoration : public VariableDecoration {
uint32_t value() const { return value_; } uint32_t value() const { return value_; }
/// Outputs the decoration to the given stream /// Outputs the decoration to the given stream
/// @param out the stream to output too /// @param out the stream to write to
void to_str(std::ostream& out) const override; /// @param indent number of spaces to indent the node when writing
void to_str(std::ostream& out, size_t indent) const override;
private: private:
uint32_t value_ = 0; uint32_t value_ = 0;

View File

@ -39,7 +39,7 @@ TEST_F(ConstantIdDecorationTest, Is) {
TEST_F(ConstantIdDecorationTest, ToStr) { TEST_F(ConstantIdDecorationTest, ToStr) {
ConstantIdDecoration d{1200, Source{}}; ConstantIdDecoration d{1200, Source{}};
std::ostringstream out; std::ostringstream out;
d.to_str(out); d.to_str(out, 0);
EXPECT_EQ(out.str(), R"(ConstantIdDecoration{1200} EXPECT_EQ(out.str(), R"(ConstantIdDecoration{1200}
)"); )");
} }

View File

@ -86,8 +86,7 @@ void DecoratedVariable::to_str(std::ostream& out, size_t indent) const {
make_indent(out, indent + 2); make_indent(out, indent + 2);
out << "Decorations{" << std::endl; out << "Decorations{" << std::endl;
for (const auto& deco : decorations_) { for (const auto& deco : decorations_) {
make_indent(out, indent + 4); deco->to_str(out, indent + 4);
deco->to_str(out);
} }
make_indent(out, indent + 2); make_indent(out, indent + 2);

View File

@ -35,5 +35,9 @@ std::ostream& operator<<(std::ostream& out, DecorationKind data) {
return out << "<unknown>"; return out << "<unknown>";
} }
bool Decoration::IsValid() const {
return true;
}
} // namespace ast } // namespace ast
} // namespace tint } // namespace tint

View File

@ -19,6 +19,7 @@
#include <ostream> #include <ostream>
#include <vector> #include <vector>
#include "src/ast/node.h"
#include "src/source.h" #include "src/source.h"
namespace tint { namespace tint {
@ -36,9 +37,9 @@ enum class DecorationKind {
std::ostream& operator<<(std::ostream& out, DecorationKind data); std::ostream& operator<<(std::ostream& out, DecorationKind data);
/// The base class for all decorations /// The base class for all decorations
class Decoration { class Decoration : public Node {
public: public:
virtual ~Decoration(); ~Decoration() override;
/// @return the decoration kind /// @return the decoration kind
DecorationKind GetKind() const { return kind_; } DecorationKind GetKind() const { return kind_; }
@ -49,19 +50,18 @@ class Decoration {
return GetKind() == TO::Kind; return GetKind() == TO::Kind;
} }
/// @return the source of this decoration /// @returns true if the node is valid
const Source& GetSource() { return source_; } bool IsValid() const override;
protected: protected:
/// Constructor /// Constructor
/// @param kind represents the derived type /// @param kind represents the derived type
/// @param source the source of this decoration /// @param source the source of this decoration
Decoration(DecorationKind kind, const Source& source) Decoration(DecorationKind kind, const Source& source)
: kind_(kind), source_(source) {} : Node(source), kind_(kind) {}
private: private:
DecorationKind const kind_; DecorationKind const kind_;
Source const source_;
}; };
/// As dynamically casts |deco| to the target type |TO|. /// As dynamically casts |deco| to the target type |TO|.

View File

@ -233,8 +233,7 @@ void Function::to_str(std::ostream& out, size_t indent) const {
<< std::endl; << std::endl;
for (const auto& deco : decorations()) { for (const auto& deco : decorations()) {
make_indent(out, indent); deco->to_str(out, indent);
deco->to_str(out);
} }
make_indent(out, indent); make_indent(out, indent);

View File

@ -45,10 +45,6 @@ class FunctionDecoration : public Decoration {
/// @returns the decoration as a workgroup decoration /// @returns the decoration as a workgroup decoration
const WorkgroupDecoration* AsWorkgroup() const; const WorkgroupDecoration* AsWorkgroup() const;
/// Outputs the function decoration to the given stream
/// @param out the stream to output too
virtual void to_str(std::ostream& out) const = 0;
protected: protected:
/// Constructor /// Constructor
/// @param source the source of this decoration /// @param source the source of this decoration

View File

@ -84,5 +84,14 @@ UintLiteral* Literal::AsUint() {
return static_cast<UintLiteral*>(this); return static_cast<UintLiteral*>(this);
} }
bool Literal::IsValid() const {
return true;
}
void Literal::to_str(std::ostream& out, size_t indent) const {
make_indent(out, indent);
out << to_str();
}
} // namespace ast } // namespace ast
} // namespace tint } // namespace tint

View File

@ -17,6 +17,7 @@
#include <string> #include <string>
#include "src/ast/node.h"
#include "src/ast/type/type.h" #include "src/ast/type/type.h"
namespace tint { namespace tint {
@ -30,9 +31,9 @@ class IntLiteral;
class UintLiteral; class UintLiteral;
/// Base class for a literal value /// Base class for a literal value
class Literal { class Literal : public Node {
public: public:
virtual ~Literal(); ~Literal() override;
/// @returns true if this is a bool literal /// @returns true if this is a bool literal
virtual bool IsBool() const; virtual bool IsBool() const;
@ -63,6 +64,14 @@ class Literal {
/// @returns the type of the literal /// @returns the type of the literal
ast::type::Type* type() const { return type_; } ast::type::Type* type() const { return type_; }
/// @returns true if the node is valid
bool IsValid() const override;
/// Writes a representation of the node to the output stream
/// @param out the stream to write to
/// @param indent number of spaces to indent the node when writing
void to_str(std::ostream& out, size_t indent) const override;
/// @returns the literal as a string /// @returns the literal as a string
virtual std::string to_str() const = 0; virtual std::string to_str() const = 0;

View File

@ -26,7 +26,8 @@ bool LocationDecoration::IsLocation() const {
return true; return true;
} }
void LocationDecoration::to_str(std::ostream& out) const { void LocationDecoration::to_str(std::ostream& out, size_t indent) const {
make_indent(out, indent);
out << "LocationDecoration{" << value_ << "}" << std::endl; out << "LocationDecoration{" << value_ << "}" << std::endl;
} }

View File

@ -38,8 +38,9 @@ class LocationDecoration : public VariableDecoration {
uint32_t value() const { return value_; } uint32_t value() const { return value_; }
/// Outputs the decoration to the given stream /// Outputs the decoration to the given stream
/// @param out the stream to output too /// @param out the stream to write to
void to_str(std::ostream& out) const override; /// @param indent number of spaces to indent the node when writing
void to_str(std::ostream& out, size_t indent) const override;
private: private:
uint32_t value_; uint32_t value_;

View File

@ -41,7 +41,7 @@ TEST_F(LocationDecorationTest, Is) {
TEST_F(LocationDecorationTest, ToStr) { TEST_F(LocationDecorationTest, ToStr) {
LocationDecoration d{2, Source{}}; LocationDecoration d{2, Source{}};
std::ostringstream out; std::ostringstream out;
d.to_str(out); d.to_str(out, 0);
EXPECT_EQ(out.str(), R"(LocationDecoration{2} EXPECT_EQ(out.str(), R"(LocationDecoration{2}
)"); )");
} }

View File

@ -26,7 +26,8 @@ bool SetDecoration::IsSet() const {
return true; return true;
} }
void SetDecoration::to_str(std::ostream& out) const { void SetDecoration::to_str(std::ostream& out, size_t indent) const {
make_indent(out, indent);
out << "SetDecoration{" << value_ << "}" << std::endl; out << "SetDecoration{" << value_ << "}" << std::endl;
} }

View File

@ -38,8 +38,9 @@ class SetDecoration : public VariableDecoration {
uint32_t value() const { return value_; } uint32_t value() const { return value_; }
/// Outputs the decoration to the given stream /// Outputs the decoration to the given stream
/// @param out the stream to output too /// @param out the stream to write to
void to_str(std::ostream& out) const override; /// @param indent number of spaces to indent the node when writing
void to_str(std::ostream& out, size_t indent) const override;
private: private:
uint32_t value_; uint32_t value_;

View File

@ -39,7 +39,7 @@ TEST_F(SetDecorationTest, Is) {
TEST_F(SetDecorationTest, ToStr) { TEST_F(SetDecorationTest, ToStr) {
SetDecoration d{2, Source{}}; SetDecoration d{2, Source{}};
std::ostringstream out; std::ostringstream out;
d.to_str(out); d.to_str(out, 0);
EXPECT_EQ(out.str(), R"(SetDecoration{2} EXPECT_EQ(out.str(), R"(SetDecoration{2}
)"); )");
} }

View File

@ -26,7 +26,8 @@ bool StageDecoration::IsStage() const {
return true; return true;
} }
void StageDecoration::to_str(std::ostream& out) const { void StageDecoration::to_str(std::ostream& out, size_t indent) const {
make_indent(out, indent);
out << "StageDecoration{" << stage_ << "}" << std::endl; out << "StageDecoration{" << stage_ << "}" << std::endl;
} }

View File

@ -37,8 +37,9 @@ class StageDecoration : public FunctionDecoration {
ast::PipelineStage value() const { return stage_; } ast::PipelineStage value() const { return stage_; }
/// Outputs the decoration to the given stream /// Outputs the decoration to the given stream
/// @param out the stream to output too /// @param out the stream to write to
void to_str(std::ostream& out) const override; /// @param indent number of spaces to indent the node when writing
void to_str(std::ostream& out, size_t indent) const override;
private: private:
ast::PipelineStage stage_ = ast::PipelineStage::kNone; ast::PipelineStage stage_ = ast::PipelineStage::kNone;

View File

@ -38,7 +38,7 @@ TEST_F(StageDecorationTest, Is) {
TEST_F(StageDecorationTest, ToStr) { TEST_F(StageDecorationTest, ToStr) {
StageDecoration d{ast::PipelineStage::kFragment, Source{}}; StageDecoration d{ast::PipelineStage::kFragment, Source{}};
std::ostringstream out; std::ostringstream out;
d.to_str(out); d.to_str(out, 0);
EXPECT_EQ(out.str(), R"(StageDecoration{fragment} EXPECT_EQ(out.str(), R"(StageDecoration{fragment}
)"); )");
} }

View File

@ -26,8 +26,9 @@ bool StrideDecoration::IsStride() const {
StrideDecoration::~StrideDecoration() = default; StrideDecoration::~StrideDecoration() = default;
std::string StrideDecoration::to_str() const { void StrideDecoration::to_str(std::ostream& out, size_t indent) const {
return "stride " + std::to_string(stride_); make_indent(out, indent);
out << "stride " << stride_;
} }
} // namespace ast } // namespace ast

View File

@ -39,8 +39,10 @@ class StrideDecoration : public ArrayDecoration {
/// @returns the stride value /// @returns the stride value
uint32_t stride() const { return stride_; } uint32_t stride() const { return stride_; }
/// @returns the decoration as a string /// Outputs the decoration to the given stream
std::string to_str() const override; /// @param out the stream to write to
/// @param indent number of spaces to indent the node when writing
void to_str(std::ostream& out, size_t indent) const override;
private: private:
uint32_t stride_; uint32_t stride_;

View File

@ -35,10 +35,10 @@ TEST_F(StrideDecorationTest, Is) {
TEST_F(StrideDecorationTest, Source) { TEST_F(StrideDecorationTest, Source) {
StrideDecoration d{ StrideDecoration d{
2, Source{Source::Range{Source::Location{1, 2}, Source::Location{3, 4}}}}; 2, Source{Source::Range{Source::Location{1, 2}, Source::Location{3, 4}}}};
EXPECT_EQ(d.GetSource().range.begin.line, 1u); EXPECT_EQ(d.source().range.begin.line, 1u);
EXPECT_EQ(d.GetSource().range.begin.column, 2u); EXPECT_EQ(d.source().range.begin.column, 2u);
EXPECT_EQ(d.GetSource().range.end.line, 3u); EXPECT_EQ(d.source().range.end.line, 3u);
EXPECT_EQ(d.GetSource().range.end.column, 4u); EXPECT_EQ(d.source().range.end.column, 4u);
} }
} // namespace } // namespace

View File

@ -73,7 +73,7 @@ void Struct::to_str(std::ostream& out, size_t indent) const {
for (auto& deco : decorations_) { for (auto& deco : decorations_) {
make_indent(out, indent + 2); make_indent(out, indent + 2);
out << "[["; out << "[[";
deco->to_str(out); deco->to_str(out, 0);
out << "]]" << std::endl; out << "]]" << std::endl;
} }
for (const auto& member : members_) { for (const auto& member : members_) {

View File

@ -26,7 +26,8 @@ bool StructBlockDecoration::IsBlock() const {
return true; return true;
} }
void StructBlockDecoration::to_str(std::ostream& out) const { void StructBlockDecoration::to_str(std::ostream& out, size_t indent) const {
make_indent(out, indent);
out << "block"; out << "block";
} }

View File

@ -36,8 +36,9 @@ class StructBlockDecoration : public StructDecoration {
bool IsBlock() const override; bool IsBlock() const override;
/// Outputs the decoration to the given stream /// Outputs the decoration to the given stream
/// @param out the stream to output too /// @param out the stream to write to
void to_str(std::ostream& out) const override; /// @param indent number of spaces to indent the node when writing
void to_str(std::ostream& out, size_t indent) const override;
}; };
/// List of struct decorations /// List of struct decorations

View File

@ -35,10 +35,6 @@ class StructDecoration : public Decoration {
/// @returns true if this is a block struct /// @returns true if this is a block struct
virtual bool IsBlock() const = 0; virtual bool IsBlock() const = 0;
/// Outputs the decoration to the given stream
/// @param out the stream to output too
virtual void to_str(std::ostream& out) const = 0;
protected: protected:
/// Constructor /// Constructor
/// @param source the source of this decoration /// @param source the source of this decoration

View File

@ -75,7 +75,7 @@ void StructMember::to_str(std::ostream& out, size_t indent) const {
if (decorations_.size() > 0) { if (decorations_.size() > 0) {
out << "[[ "; out << "[[ ";
for (const auto& deco : decorations_) for (const auto& deco : decorations_)
out << deco->to_str() << " "; out << deco->str() << " ";
out << "]] "; out << "]] ";
} }

View File

@ -40,9 +40,6 @@ class StructMemberDecoration : public Decoration {
/// @returns the decoration as an offset decoration /// @returns the decoration as an offset decoration
StructMemberOffsetDecoration* AsOffset(); StructMemberOffsetDecoration* AsOffset();
/// @returns the decoration as a string
virtual std::string to_str() const = 0;
protected: protected:
/// Constructor /// Constructor
/// @param source the source of this decoration /// @param source the source of this decoration

View File

@ -27,8 +27,10 @@ bool StructMemberOffsetDecoration::IsOffset() const {
StructMemberOffsetDecoration::~StructMemberOffsetDecoration() = default; StructMemberOffsetDecoration::~StructMemberOffsetDecoration() = default;
std::string StructMemberOffsetDecoration::to_str() const { void StructMemberOffsetDecoration::to_str(std::ostream& out,
return "offset " + std::to_string(offset_); size_t indent) const {
make_indent(out, indent);
out << "offset " << std::to_string(offset_);
} }
} // namespace ast } // namespace ast

View File

@ -39,8 +39,10 @@ class StructMemberOffsetDecoration : public StructMemberDecoration {
/// @returns the offset value /// @returns the offset value
uint32_t offset() const { return offset_; } uint32_t offset() const { return offset_; }
/// @returns the decoration as a string /// Outputs the decoration to the given stream
std::string to_str() const override; /// @param out the stream to write to
/// @param indent number of spaces to indent the node when writing
void to_str(std::ostream& out, size_t indent) const override;
private: private:
uint32_t offset_; uint32_t offset_;

View File

@ -61,10 +61,6 @@ class VariableDecoration : public Decoration {
/// @returns the decoration as a set decoration /// @returns the decoration as a set decoration
SetDecoration* AsSet(); SetDecoration* AsSet();
/// Outputs the variable decoration to the given stream
/// @param out the stream to output too
virtual void to_str(std::ostream& out) const = 0;
protected: protected:
/// Constructor /// Constructor
/// @param source the source of this decoration /// @param source the source of this decoration

View File

@ -37,7 +37,8 @@ bool WorkgroupDecoration::IsWorkgroup() const {
return true; return true;
} }
void WorkgroupDecoration::to_str(std::ostream& out) const { void WorkgroupDecoration::to_str(std::ostream& out, size_t indent) const {
make_indent(out, indent);
out << "WorkgroupDecoration{" << x_ << " " << y_ << " " << z_ << "}" out << "WorkgroupDecoration{" << x_ << " " << y_ << " " << z_ << "}"
<< std::endl; << std::endl;
} }

View File

@ -53,8 +53,9 @@ class WorkgroupDecoration : public FunctionDecoration {
} }
/// Outputs the decoration to the given stream /// Outputs the decoration to the given stream
/// @param out the stream to output too /// @param out the stream to write to
void to_str(std::ostream& out) const override; /// @param indent number of spaces to indent the node when writing
void to_str(std::ostream& out, size_t indent) const override;
private: private:
uint32_t x_ = 1; uint32_t x_ = 1;

View File

@ -65,7 +65,7 @@ TEST_F(WorkgroupDecorationTest, Is) {
TEST_F(WorkgroupDecorationTest, ToStr) { TEST_F(WorkgroupDecorationTest, ToStr) {
WorkgroupDecoration d{2, 4, 6, Source{}}; WorkgroupDecoration d{2, 4, 6, Source{}};
std::ostringstream out; std::ostringstream out;
d.to_str(out); d.to_str(out, 0);
EXPECT_EQ(out.str(), R"(WorkgroupDecoration{2 4 6} EXPECT_EQ(out.str(), R"(WorkgroupDecoration{2 4 6}
)"); )");
} }

View File

@ -2844,7 +2844,7 @@ Expect<std::vector<std::unique_ptr<T>>> ParserImpl::cast_decorations(
std::stringstream msg; std::stringstream msg;
msg << deco->GetKind() << " decoration type cannot be used for " msg << deco->GetKind() << " decoration type cannot be used for "
<< T::Kind; << T::Kind;
add_error(deco->GetSource(), msg.str()); add_error(deco->source(), msg.str());
ok = false; ok = false;
continue; continue;
} }
@ -2864,7 +2864,7 @@ bool ParserImpl::expect_decorations_consumed(const ast::DecorationList& in) {
if (in.empty()) { if (in.empty()) {
return true; return true;
} }
add_error(in[0]->GetSource(), "unexpected decorations"); add_error(in[0]->source(), "unexpected decorations");
return false; return false;
} }

View File

@ -1856,7 +1856,7 @@ bool GeneratorImpl::EmitStructType(const ast::type::StructType* str) {
} }
current_offset = offset; current_offset = offset;
} else { } else {
error_ = "unsupported member decoration: " + deco->to_str(); error_ = "unsupported member decoration: " + deco->str();
return false; return false;
} }
} }

View File

@ -573,7 +573,7 @@ bool GeneratorImpl::EmitStructType(const ast::type::StructType* str) {
auto* impl = str->impl(); auto* impl = str->impl();
for (auto& deco : impl->decorations()) { for (auto& deco : impl->decorations()) {
out_ << "[["; out_ << "[[";
deco->to_str(out_); deco->to_str(out_, 0);
out_ << "]]" << std::endl; out_ << "]]" << std::endl;
} }
out_ << "struct " << str->name() << " {" << std::endl; out_ << "struct " << str->name() << " {" << std::endl;