Introduce semantic::Info

Will hold the mutable fields that currently reside in the otherwise immutable-AST.

Change the AST string methods to accept a `const semantic::Info&`. This is required as some nodes include type-resolved information in their output strings.

Bug: tint:390
Change-Id: Iba494a9c5645ce2096da0a8cfe63a4309a9d9c3c
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/39003
Commit-Queue: Ben Clayton <bclayton@google.com>
Reviewed-by: dan sinclair <dsinclair@chromium.org>
This commit is contained in:
Ben Clayton 2021-01-29 10:55:40 +00:00 committed by Commit Bot service account
parent 63049a420e
commit dd1b6fca9f
174 changed files with 1105 additions and 827 deletions

View File

@ -378,6 +378,8 @@ source_set("libtint_core_src") {
"src/reader/reader.cc", "src/reader/reader.cc",
"src/reader/reader.h", "src/reader/reader.h",
"src/scope_stack.h", "src/scope_stack.h",
"src/semantic/info.h",
"src/semantic/sem_info.cc",
"src/source.cc", "src/source.cc",
"src/source.h", "src/source.h",
"src/symbol.cc", "src/symbol.cc",
@ -778,7 +780,6 @@ source_set("tint_unittests_core_src") {
"src/ast/decoration_test.cc", "src/ast/decoration_test.cc",
"src/ast/discard_statement_test.cc", "src/ast/discard_statement_test.cc",
"src/ast/else_statement_test.cc", "src/ast/else_statement_test.cc",
"src/ast/expression_test.cc",
"src/ast/fallthrough_statement_test.cc", "src/ast/fallthrough_statement_test.cc",
"src/ast/float_literal_test.cc", "src/ast/float_literal_test.cc",
"src/ast/function_test.cc", "src/ast/function_test.cc",

View File

@ -192,6 +192,8 @@ set(TINT_LIB_SRCS
reader/reader.cc reader/reader.cc
reader/reader.h reader/reader.h
scope_stack.h scope_stack.h
semantic/info.h
semantic/sem_info.cc
source.cc source.cc
source.h source.h
symbol.cc symbol.cc
@ -408,7 +410,6 @@ if(${TINT_BUILD_TESTS})
ast/decoration_test.cc ast/decoration_test.cc
ast/discard_statement_test.cc ast/discard_statement_test.cc
ast/else_statement_test.cc ast/else_statement_test.cc
ast/expression_test.cc
ast/fallthrough_statement_test.cc ast/fallthrough_statement_test.cc
ast/float_literal_test.cc ast/float_literal_test.cc
ast/function_test.cc ast/function_test.cc

View File

@ -27,7 +27,9 @@ AccessDecoration::AccessDecoration(const Source& source, AccessControl val)
AccessDecoration::~AccessDecoration() = default; AccessDecoration::~AccessDecoration() = default;
void AccessDecoration::to_str(std::ostream& out, size_t indent) const { void AccessDecoration::to_str(const semantic::Info&,
std::ostream& out,
size_t indent) const {
make_indent(out, indent); make_indent(out, indent);
out << "AccessDecoration{" << value_ << "}" << std::endl; out << "AccessDecoration{" << value_ << "}" << std::endl;
} }

View File

@ -36,9 +36,12 @@ class AccessDecoration : public Castable<AccessDecoration, TypeDecoration> {
AccessControl value() const { return value_; } AccessControl value() const { return value_; }
/// Outputs the decoration to the given stream /// Outputs the decoration to the given stream
/// @param sem the semantic info for the program
/// @param out the stream to write to /// @param out the stream to write to
/// @param indent number of spaces to indent the node when writing /// @param indent number of spaces to indent the node when writing
void to_str(std::ostream& out, size_t indent) const override; void to_str(const semantic::Info& sem,
std::ostream& out,
size_t indent) const override;
/// Clones this node and all transitive child nodes using the `CloneContext` /// Clones this node and all transitive child nodes using the `CloneContext`
/// `ctx`. /// `ctx`.

View File

@ -37,7 +37,7 @@ TEST_F(AccessDecorationTest, Is) {
TEST_F(AccessDecorationTest, ToStr) { TEST_F(AccessDecorationTest, ToStr) {
auto* d = create<AccessDecoration>(ast::AccessControl::kReadOnly); auto* d = create<AccessDecoration>(ast::AccessControl::kReadOnly);
std::ostringstream out; std::ostringstream out;
d->to_str(out, 0); d->to_str(Sem(), out, 0);
EXPECT_EQ(out.str(), R"(AccessDecoration{read_only} EXPECT_EQ(out.str(), R"(AccessDecoration{read_only}
)"); )");
} }

View File

@ -47,11 +47,13 @@ bool ArrayAccessorExpression::IsValid() const {
return true; return true;
} }
void ArrayAccessorExpression::to_str(std::ostream& out, size_t indent) const { void ArrayAccessorExpression::to_str(const semantic::Info& sem,
std::ostream& out,
size_t indent) const {
make_indent(out, indent); make_indent(out, indent);
out << "ArrayAccessor[" << result_type_str() << "]{" << std::endl; out << "ArrayAccessor[" << result_type_str(sem) << "]{" << std::endl;
array_->to_str(out, indent + 2); array_->to_str(sem, out, indent + 2);
idx_expr_->to_str(out, indent + 2); idx_expr_->to_str(sem, out, indent + 2);
make_indent(out, indent); make_indent(out, indent);
out << "}" << std::endl; out << "}" << std::endl;
} }

View File

@ -57,9 +57,12 @@ class ArrayAccessorExpression
bool IsValid() const override; bool IsValid() const override;
/// Writes a representation of the node to the output stream /// Writes a representation of the node to the output stream
/// @param sem the semantic info for the program
/// @param out the stream to write to /// @param out the stream to write to
/// @param indent number of spaces to indent the node when writing /// @param indent number of spaces to indent the node when writing
void to_str(std::ostream& out, size_t indent) const override; void to_str(const semantic::Info& sem,
std::ostream& out,
size_t indent) const override;
private: private:
ArrayAccessorExpression(const ArrayAccessorExpression&) = delete; ArrayAccessorExpression(const ArrayAccessorExpression&) = delete;

View File

@ -93,7 +93,7 @@ TEST_F(ArrayAccessorExpressionTest, ToStr) {
auto* exp = create<ArrayAccessorExpression>(ary, idx); auto* exp = create<ArrayAccessorExpression>(ary, idx);
std::ostringstream out; std::ostringstream out;
exp->to_str(out, 2); exp->to_str(Sem(), out, 2);
EXPECT_EQ(demangle(out.str()), R"( ArrayAccessor[not set]{ EXPECT_EQ(demangle(out.str()), R"( ArrayAccessor[not set]{
Identifier[not set]{ary} Identifier[not set]{ary}

View File

@ -45,11 +45,13 @@ bool AssignmentStatement::IsValid() const {
return true; return true;
} }
void AssignmentStatement::to_str(std::ostream& out, size_t indent) const { void AssignmentStatement::to_str(const semantic::Info& sem,
std::ostream& out,
size_t indent) const {
make_indent(out, indent); make_indent(out, indent);
out << "Assignment{" << std::endl; out << "Assignment{" << std::endl;
lhs_->to_str(out, indent + 2); lhs_->to_str(sem, out, indent + 2);
rhs_->to_str(out, indent + 2); rhs_->to_str(sem, out, indent + 2);
make_indent(out, indent); make_indent(out, indent);
out << "}" << std::endl; out << "}" << std::endl;
} }

View File

@ -54,9 +54,12 @@ class AssignmentStatement : public Castable<AssignmentStatement, Statement> {
bool IsValid() const override; bool IsValid() const override;
/// Writes a representation of the node to the output stream /// Writes a representation of the node to the output stream
/// @param sem the semantic info for the program
/// @param out the stream to write to /// @param out the stream to write to
/// @param indent number of spaces to indent the node when writing /// @param indent number of spaces to indent the node when writing
void to_str(std::ostream& out, size_t indent) const override; void to_str(const semantic::Info& sem,
std::ostream& out,
size_t indent) const override;
private: private:
AssignmentStatement(const AssignmentStatement&) = delete; AssignmentStatement(const AssignmentStatement&) = delete;

View File

@ -93,7 +93,7 @@ TEST_F(AssignmentStatementTest, ToStr) {
auto* stmt = create<AssignmentStatement>(lhs, rhs); auto* stmt = create<AssignmentStatement>(lhs, rhs);
std::ostringstream out; std::ostringstream out;
stmt->to_str(out, 2); stmt->to_str(Sem(), out, 2);
EXPECT_EQ(demangle(out.str()), R"( Assignment{ EXPECT_EQ(demangle(out.str()), R"( Assignment{
Identifier[not set]{lhs} Identifier[not set]{lhs}

View File

@ -47,15 +47,17 @@ bool BinaryExpression::IsValid() const {
return op_ != BinaryOp::kNone; return op_ != BinaryOp::kNone;
} }
void BinaryExpression::to_str(std::ostream& out, size_t indent) const { void BinaryExpression::to_str(const semantic::Info& sem,
std::ostream& out,
size_t indent) const {
make_indent(out, indent); make_indent(out, indent);
out << "Binary[" << result_type_str() << "]{" << std::endl; out << "Binary[" << result_type_str(sem) << "]{" << std::endl;
lhs_->to_str(out, indent + 2); lhs_->to_str(sem, out, indent + 2);
make_indent(out, indent + 2); make_indent(out, indent + 2);
out << op_ << std::endl; out << op_ << std::endl;
rhs_->to_str(out, indent + 2); rhs_->to_str(sem, out, indent + 2);
make_indent(out, indent); make_indent(out, indent);
out << "}" << std::endl; out << "}" << std::endl;
} }

View File

@ -120,9 +120,12 @@ class BinaryExpression : public Castable<BinaryExpression, Expression> {
bool IsValid() const override; bool IsValid() const override;
/// Writes a representation of the node to the output stream /// Writes a representation of the node to the output stream
/// @param sem the semantic info for the program
/// @param out the stream to write to /// @param out the stream to write to
/// @param indent number of spaces to indent the node when writing /// @param indent number of spaces to indent the node when writing
void to_str(std::ostream& out, size_t indent) const override; void to_str(const semantic::Info& sem,
std::ostream& out,
size_t indent) const override;
private: private:
BinaryExpression(const BinaryExpression&) = delete; BinaryExpression(const BinaryExpression&) = delete;

View File

@ -106,7 +106,7 @@ TEST_F(BinaryExpressionTest, ToStr) {
auto* r = create<BinaryExpression>(BinaryOp::kEqual, lhs, rhs); auto* r = create<BinaryExpression>(BinaryOp::kEqual, lhs, rhs);
std::ostringstream out; std::ostringstream out;
r->to_str(out, 2); r->to_str(Sem(), out, 2);
EXPECT_EQ(demangle(out.str()), R"( Binary[not set]{ EXPECT_EQ(demangle(out.str()), R"( Binary[not set]{
Identifier[not set]{lhs} Identifier[not set]{lhs}
equal equal

View File

@ -27,7 +27,9 @@ BindingDecoration::BindingDecoration(const Source& source, uint32_t val)
BindingDecoration::~BindingDecoration() = default; BindingDecoration::~BindingDecoration() = default;
void BindingDecoration::to_str(std::ostream& out, size_t indent) const { void BindingDecoration::to_str(const semantic::Info&,
std::ostream& out,
size_t indent) const {
make_indent(out, indent); make_indent(out, indent);
out << "BindingDecoration{" << value_ << "}" << std::endl; out << "BindingDecoration{" << value_ << "}" << std::endl;
} }

View File

@ -36,9 +36,12 @@ class BindingDecoration
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 sem the semantic info for the program
/// @param out the stream to write to /// @param out the stream to write to
/// @param indent number of spaces to indent the node when writing /// @param indent number of spaces to indent the node when writing
void to_str(std::ostream& out, size_t indent) const override; void to_str(const semantic::Info& sem,
std::ostream& out,
size_t indent) const override;
/// Clones this node and all transitive child nodes using the `CloneContext` /// Clones this node and all transitive child nodes using the `CloneContext`
/// `ctx`. /// `ctx`.

View File

@ -40,7 +40,7 @@ TEST_F(BindingDecorationTest, Is) {
TEST_F(BindingDecorationTest, ToStr) { TEST_F(BindingDecorationTest, ToStr) {
auto* d = create<BindingDecoration>(2); auto* d = create<BindingDecoration>(2);
std::ostringstream out; std::ostringstream out;
d->to_str(out, 0); d->to_str(Sem(), out, 0);
EXPECT_EQ(out.str(), R"(BindingDecoration{2} EXPECT_EQ(out.str(), R"(BindingDecoration{2}
)"); )");
} }

View File

@ -41,11 +41,13 @@ bool BitcastExpression::IsValid() const {
return type_ != nullptr; return type_ != nullptr;
} }
void BitcastExpression::to_str(std::ostream& out, size_t indent) const { void BitcastExpression::to_str(const semantic::Info& sem,
std::ostream& out,
size_t indent) const {
make_indent(out, indent); make_indent(out, indent);
out << "Bitcast[" << result_type_str() << "]<" << type_->type_name() << ">{" out << "Bitcast[" << result_type_str(sem) << "]<" << type_->type_name()
<< std::endl; << ">{" << std::endl;
expr_->to_str(out, indent + 2); expr_->to_str(sem, out, indent + 2);
make_indent(out, indent); make_indent(out, indent);
out << "}" << std::endl; out << "}" << std::endl;
} }

View File

@ -54,9 +54,12 @@ class BitcastExpression : public Castable<BitcastExpression, Expression> {
bool IsValid() const override; bool IsValid() const override;
/// Writes a representation of the node to the output stream /// Writes a representation of the node to the output stream
/// @param sem the semantic info for the program
/// @param out the stream to write to /// @param out the stream to write to
/// @param indent number of spaces to indent the node when writing /// @param indent number of spaces to indent the node when writing
void to_str(std::ostream& out, size_t indent) const override; void to_str(const semantic::Info& sem,
std::ostream& out,
size_t indent) const override;
private: private:
BitcastExpression(const BitcastExpression&) = delete; BitcastExpression(const BitcastExpression&) = delete;

View File

@ -79,7 +79,7 @@ TEST_F(BitcastExpressionTest, ToStr) {
auto* exp = create<BitcastExpression>(ty.f32(), expr); auto* exp = create<BitcastExpression>(ty.f32(), expr);
std::ostringstream out; std::ostringstream out;
exp->to_str(out, 2); exp->to_str(Sem(), out, 2);
EXPECT_EQ(demangle(out.str()), R"( Bitcast[not set]<__f32>{ EXPECT_EQ(demangle(out.str()), R"( Bitcast[not set]<__f32>{
Identifier[not set]{expr} Identifier[not set]{expr}

View File

@ -44,12 +44,14 @@ bool BlockStatement::IsValid() const {
return true; return true;
} }
void BlockStatement::to_str(std::ostream& out, size_t indent) const { void BlockStatement::to_str(const semantic::Info& sem,
std::ostream& out,
size_t indent) const {
make_indent(out, indent); make_indent(out, indent);
out << "Block{" << std::endl; out << "Block{" << std::endl;
for (auto* stmt : *this) { for (auto* stmt : *this) {
stmt->to_str(out, indent + 2); stmt->to_str(sem, out, indent + 2);
} }
make_indent(out, indent); make_indent(out, indent);

View File

@ -76,9 +76,12 @@ class BlockStatement : public Castable<BlockStatement, Statement> {
bool IsValid() const override; bool IsValid() const override;
/// Writes a representation of the node to the output stream /// Writes a representation of the node to the output stream
/// @param sem the semantic info for the program
/// @param out the stream to write to /// @param out the stream to write to
/// @param indent number of spaces to indent the node when writing /// @param indent number of spaces to indent the node when writing
void to_str(std::ostream& out, size_t indent) const override; void to_str(const semantic::Info& sem,
std::ostream& out,
size_t indent) const override;
private: private:
BlockStatement(const BlockStatement&) = delete; BlockStatement(const BlockStatement&) = delete;

View File

@ -87,7 +87,7 @@ TEST_F(BlockStatementTest, ToStr) {
}); });
std::ostringstream out; std::ostringstream out;
b->to_str(out, 2); b->to_str(Sem(), out, 2);
EXPECT_EQ(out.str(), R"( Block{ EXPECT_EQ(out.str(), R"( Block{
Discard{} Discard{}
} }

View File

@ -27,7 +27,7 @@ BoolLiteral::BoolLiteral(const Source& source, type::Type* type, bool value)
BoolLiteral::~BoolLiteral() = default; BoolLiteral::~BoolLiteral() = default;
std::string BoolLiteral::to_str() const { std::string BoolLiteral::to_str(const semantic::Info&) const {
return value_ ? "true" : "false"; return value_ ? "true" : "false";
} }

View File

@ -40,8 +40,9 @@ class BoolLiteral : public Castable<BoolLiteral, Literal> {
/// @returns the name for this literal. This name is unique to this value. /// @returns the name for this literal. This name is unique to this value.
std::string name() const override; std::string name() const override;
/// @param sem the semantic info for the program
/// @returns the literal as a string /// @returns the literal as a string
std::string to_str() const override; std::string to_str(const semantic::Info& sem) const override;
/// Clones this node and all transitive child nodes using the `CloneContext` /// Clones this node and all transitive child nodes using the `CloneContext`
/// `ctx`. /// `ctx`.

View File

@ -59,8 +59,8 @@ TEST_F(BoolLiteralTest, ToStr) {
auto* t = create<BoolLiteral>(&bool_type, true); auto* t = create<BoolLiteral>(&bool_type, true);
auto* f = create<BoolLiteral>(&bool_type, false); auto* f = create<BoolLiteral>(&bool_type, false);
EXPECT_EQ(t->to_str(), "true"); EXPECT_EQ(t->to_str(Sem()), "true");
EXPECT_EQ(f->to_str(), "false"); EXPECT_EQ(f->to_str(Sem()), "false");
} }
} // namespace } // namespace

View File

@ -36,7 +36,9 @@ bool BreakStatement::IsValid() const {
return true; return true;
} }
void BreakStatement::to_str(std::ostream& out, size_t indent) const { void BreakStatement::to_str(const semantic::Info&,
std::ostream& out,
size_t indent) const {
make_indent(out, indent); make_indent(out, indent);
out << "Break{}" << std::endl; out << "Break{}" << std::endl;
} }

View File

@ -42,9 +42,12 @@ class BreakStatement : public Castable<BreakStatement, Statement> {
bool IsValid() const override; bool IsValid() const override;
/// Writes a representation of the node to the output stream /// Writes a representation of the node to the output stream
/// @param sem the semantic info for the program
/// @param out the stream to write to /// @param out the stream to write to
/// @param indent number of spaces to indent the node when writing /// @param indent number of spaces to indent the node when writing
void to_str(std::ostream& out, size_t indent) const override; void to_str(const semantic::Info& sem,
std::ostream& out,
size_t indent) const override;
private: private:
BreakStatement(const BreakStatement&) = delete; BreakStatement(const BreakStatement&) = delete;

View File

@ -42,7 +42,7 @@ TEST_F(BreakStatementTest, IsValid) {
TEST_F(BreakStatementTest, ToStr) { TEST_F(BreakStatementTest, ToStr) {
auto* stmt = create<BreakStatement>(); auto* stmt = create<BreakStatement>();
std::ostringstream out; std::ostringstream out;
stmt->to_str(out, 2); stmt->to_str(Sem(), out, 2);
EXPECT_EQ(out.str(), R"( Break{} EXPECT_EQ(out.str(), R"( Break{}
)"); )");
} }

View File

@ -27,7 +27,9 @@ BuiltinDecoration::BuiltinDecoration(const Source& source, Builtin builtin)
BuiltinDecoration::~BuiltinDecoration() = default; BuiltinDecoration::~BuiltinDecoration() = default;
void BuiltinDecoration::to_str(std::ostream& out, size_t indent) const { void BuiltinDecoration::to_str(const semantic::Info&,
std::ostream& out,
size_t indent) const {
make_indent(out, indent); make_indent(out, indent);
out << "BuiltinDecoration{" << builtin_ << "}" << std::endl; out << "BuiltinDecoration{" << builtin_ << "}" << std::endl;
} }

View File

@ -35,9 +35,12 @@ class BuiltinDecoration
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 sem the semantic info for the program
/// @param out the stream to write to /// @param out the stream to write to
/// @param indent number of spaces to indent the node when writing /// @param indent number of spaces to indent the node when writing
void to_str(std::ostream& out, size_t indent) const override; void to_str(const semantic::Info& sem,
std::ostream& out,
size_t indent) const override;
/// Clones this node and all transitive child nodes using the `CloneContext` /// Clones this node and all transitive child nodes using the `CloneContext`
/// `ctx`. /// `ctx`.

View File

@ -40,7 +40,7 @@ TEST_F(BuiltinDecorationTest, Is) {
TEST_F(BuiltinDecorationTest, ToStr) { TEST_F(BuiltinDecorationTest, ToStr) {
auto* d = create<BuiltinDecoration>(Builtin::kFragDepth); auto* d = create<BuiltinDecoration>(Builtin::kFragDepth);
std::ostringstream out; std::ostringstream out;
d->to_str(out, 0); d->to_str(Sem(), out, 0);
EXPECT_EQ(out.str(), R"(BuiltinDecoration{frag_depth} EXPECT_EQ(out.str(), R"(BuiltinDecoration{frag_depth}
)"); )");
} }

View File

@ -48,15 +48,17 @@ bool CallExpression::IsValid() const {
return true; return true;
} }
void CallExpression::to_str(std::ostream& out, size_t indent) const { void CallExpression::to_str(const semantic::Info& sem,
std::ostream& out,
size_t indent) const {
make_indent(out, indent); make_indent(out, indent);
out << "Call[" << result_type_str() << "]{" << std::endl; out << "Call[" << result_type_str(sem) << "]{" << std::endl;
func_->to_str(out, indent + 2); func_->to_str(sem, out, indent + 2);
make_indent(out, indent + 2); make_indent(out, indent + 2);
out << "(" << std::endl; out << "(" << std::endl;
for (auto* param : params_) for (auto* param : params_)
param->to_str(out, indent + 4); param->to_str(sem, out, indent + 4);
make_indent(out, indent + 2); make_indent(out, indent + 2);
out << ")" << std::endl; out << ")" << std::endl;

View File

@ -53,9 +53,12 @@ class CallExpression : public Castable<CallExpression, Expression> {
bool IsValid() const override; bool IsValid() const override;
/// Writes a representation of the node to the output stream /// Writes a representation of the node to the output stream
/// @param sem the semantic info for the program
/// @param out the stream to write to /// @param out the stream to write to
/// @param indent number of spaces to indent the node when writing /// @param indent number of spaces to indent the node when writing
void to_str(std::ostream& out, size_t indent) const override; void to_str(const semantic::Info& sem,
std::ostream& out,
size_t indent) const override;
private: private:
CallExpression(const CallExpression&) = delete; CallExpression(const CallExpression&) = delete;

View File

@ -97,7 +97,7 @@ TEST_F(CallExpressionTest, ToStr_NoParams) {
auto* func = Expr("func"); auto* func = Expr("func");
auto* stmt = create<CallExpression>(func, ExpressionList{}); auto* stmt = create<CallExpression>(func, ExpressionList{});
std::ostringstream out; std::ostringstream out;
stmt->to_str(out, 2); stmt->to_str(Sem(), out, 2);
EXPECT_EQ(demangle(out.str()), R"( Call[not set]{ EXPECT_EQ(demangle(out.str()), R"( Call[not set]{
Identifier[not set]{func} Identifier[not set]{func}
( (
@ -114,7 +114,7 @@ TEST_F(CallExpressionTest, ToStr_WithParams) {
auto* stmt = create<CallExpression>(func, params); auto* stmt = create<CallExpression>(func, params);
std::ostringstream out; std::ostringstream out;
stmt->to_str(out, 2); stmt->to_str(Sem(), out, 2);
EXPECT_EQ(demangle(out.str()), R"( Call[not set]{ EXPECT_EQ(demangle(out.str()), R"( Call[not set]{
Identifier[not set]{func} Identifier[not set]{func}
( (

View File

@ -39,8 +39,10 @@ bool CallStatement::IsValid() const {
return call_ != nullptr && call_->IsValid(); return call_ != nullptr && call_->IsValid();
} }
void CallStatement::to_str(std::ostream& out, size_t indent) const { void CallStatement::to_str(const semantic::Info& sem,
call_->to_str(out, indent); std::ostream& out,
size_t indent) const {
call_->to_str(sem, out, indent);
} }
} // namespace ast } // namespace ast

View File

@ -50,9 +50,12 @@ class CallStatement : public Castable<CallStatement, Statement> {
bool IsValid() const override; bool IsValid() const override;
/// Writes a representation of the node to the output stream /// Writes a representation of the node to the output stream
/// @param sem the semantic info for the program
/// @param out the stream to write to /// @param out the stream to write to
/// @param indent number of spaces to indent the node when writing /// @param indent number of spaces to indent the node when writing
void to_str(std::ostream& out, size_t indent) const override; void to_str(const semantic::Info& sem,
std::ostream& out,
size_t indent) const override;
private: private:
CallStatement(const CallStatement&) = delete; CallStatement(const CallStatement&) = delete;

View File

@ -58,7 +58,7 @@ TEST_F(CallStatementTest, ToStr) {
create<CallExpression>(Expr("func"), ExpressionList{})); create<CallExpression>(Expr("func"), ExpressionList{}));
std::ostringstream out; std::ostringstream out;
c->to_str(out, 2); c->to_str(Sem(), out, 2);
EXPECT_EQ(demangle(out.str()), R"( Call[not set]{ EXPECT_EQ(demangle(out.str()), R"( Call[not set]{
Identifier[not set]{func} Identifier[not set]{func}
( (

View File

@ -40,7 +40,9 @@ bool CaseStatement::IsValid() const {
return body_ != nullptr && body_->IsValid(); return body_ != nullptr && body_->IsValid();
} }
void CaseStatement::to_str(std::ostream& out, size_t indent) const { void CaseStatement::to_str(const semantic::Info& sem,
std::ostream& out,
size_t indent) const {
make_indent(out, indent); make_indent(out, indent);
if (IsDefault()) { if (IsDefault()) {
@ -53,14 +55,14 @@ void CaseStatement::to_str(std::ostream& out, size_t indent) const {
out << ", "; out << ", ";
first = false; first = false;
out << selector->to_str(); out << selector->to_str(sem);
} }
out << "{" << std::endl; out << "{" << std::endl;
} }
if (body_ != nullptr) { if (body_ != nullptr) {
for (auto* stmt : *body_) { for (auto* stmt : *body_) {
stmt->to_str(out, indent + 2); stmt->to_str(sem, out, indent + 2);
} }
} }

View File

@ -66,9 +66,12 @@ class CaseStatement : public Castable<CaseStatement, Statement> {
bool IsValid() const override; bool IsValid() const override;
/// Writes a representation of the node to the output stream /// Writes a representation of the node to the output stream
/// @param sem the semantic info for the program
/// @param out the stream to write to /// @param out the stream to write to
/// @param indent number of spaces to indent the node when writing /// @param indent number of spaces to indent the node when writing
void to_str(std::ostream& out, size_t indent) const override; void to_str(const semantic::Info& sem,
std::ostream& out,
size_t indent) const override;
private: private:
CaseStatement(const CaseStatement&) = delete; CaseStatement(const CaseStatement&) = delete;

View File

@ -135,7 +135,7 @@ TEST_F(CaseStatementTest, ToStr_WithSelectors_i32) {
auto* c = create<CaseStatement>(CaseSelectorList{b}, body); auto* c = create<CaseStatement>(CaseSelectorList{b}, body);
std::ostringstream out; std::ostringstream out;
c->to_str(out, 2); c->to_str(Sem(), out, 2);
EXPECT_EQ(out.str(), R"( Case -2{ EXPECT_EQ(out.str(), R"( Case -2{
Discard{} Discard{}
} }
@ -152,7 +152,7 @@ TEST_F(CaseStatementTest, ToStr_WithSelectors_u32) {
auto* c = create<CaseStatement>(CaseSelectorList{b}, body); auto* c = create<CaseStatement>(CaseSelectorList{b}, body);
std::ostringstream out; std::ostringstream out;
c->to_str(out, 2); c->to_str(Sem(), out, 2);
EXPECT_EQ(out.str(), R"( Case 2{ EXPECT_EQ(out.str(), R"( Case 2{
Discard{} Discard{}
} }
@ -170,7 +170,7 @@ TEST_F(CaseStatementTest, ToStr_WithMultipleSelectors) {
auto* c = create<CaseStatement>(b, body); auto* c = create<CaseStatement>(b, body);
std::ostringstream out; std::ostringstream out;
c->to_str(out, 2); c->to_str(Sem(), out, 2);
EXPECT_EQ(out.str(), R"( Case 1, 2{ EXPECT_EQ(out.str(), R"( Case 1, 2{
Discard{} Discard{}
} }
@ -184,7 +184,7 @@ TEST_F(CaseStatementTest, ToStr_WithoutSelectors) {
auto* c = create<CaseStatement>(CaseSelectorList{}, body); auto* c = create<CaseStatement>(CaseSelectorList{}, body);
std::ostringstream out; std::ostringstream out;
c->to_str(out, 2); c->to_str(Sem(), out, 2);
EXPECT_EQ(out.str(), R"( Default{ EXPECT_EQ(out.str(), R"( Default{
Discard{} Discard{}
} }

View File

@ -27,7 +27,9 @@ ConstantIdDecoration::ConstantIdDecoration(const Source& source, uint32_t val)
ConstantIdDecoration::~ConstantIdDecoration() = default; ConstantIdDecoration::~ConstantIdDecoration() = default;
void ConstantIdDecoration::to_str(std::ostream& out, size_t indent) const { void ConstantIdDecoration::to_str(const semantic::Info&,
std::ostream& out,
size_t indent) const {
make_indent(out, indent); make_indent(out, indent);
out << "ConstantIdDecoration{" << value_ << "}" << std::endl; out << "ConstantIdDecoration{" << value_ << "}" << std::endl;
} }

View File

@ -35,9 +35,12 @@ class ConstantIdDecoration
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 sem the semantic info for the program
/// @param out the stream to write to /// @param out the stream to write to
/// @param indent number of spaces to indent the node when writing /// @param indent number of spaces to indent the node when writing
void to_str(std::ostream& out, size_t indent) const override; void to_str(const semantic::Info& sem,
std::ostream& out,
size_t indent) const override;
/// Clones this node and all transitive child nodes using the `CloneContext` /// Clones this node and all transitive child nodes using the `CloneContext`
/// `ctx`. /// `ctx`.

View File

@ -39,7 +39,7 @@ TEST_F(ConstantIdDecorationTest, Is) {
TEST_F(ConstantIdDecorationTest, ToStr) { TEST_F(ConstantIdDecorationTest, ToStr) {
auto* d = create<ConstantIdDecoration>(1200); auto* d = create<ConstantIdDecoration>(1200);
std::ostringstream out; std::ostringstream out;
d->to_str(out, 0); d->to_str(Sem(), out, 0);
EXPECT_EQ(out.str(), R"(ConstantIdDecoration{1200} EXPECT_EQ(out.str(), R"(ConstantIdDecoration{1200}
)"); )");
} }

View File

@ -36,7 +36,9 @@ bool ContinueStatement::IsValid() const {
return true; return true;
} }
void ContinueStatement::to_str(std::ostream& out, size_t indent) const { void ContinueStatement::to_str(const semantic::Info&,
std::ostream& out,
size_t indent) const {
make_indent(out, indent); make_indent(out, indent);
out << "Continue{}" << std::endl; out << "Continue{}" << std::endl;
} }

View File

@ -45,9 +45,12 @@ class ContinueStatement : public Castable<ContinueStatement, Statement> {
bool IsValid() const override; bool IsValid() const override;
/// Writes a representation of the node to the output stream /// Writes a representation of the node to the output stream
/// @param sem the semantic info for the program
/// @param out the stream to write to /// @param out the stream to write to
/// @param indent number of spaces to indent the node when writing /// @param indent number of spaces to indent the node when writing
void to_str(std::ostream& out, size_t indent) const override; void to_str(const semantic::Info& sem,
std::ostream& out,
size_t indent) const override;
private: private:
ContinueStatement(const ContinueStatement&) = delete; ContinueStatement(const ContinueStatement&) = delete;

View File

@ -42,7 +42,7 @@ TEST_F(ContinueStatementTest, IsValid) {
TEST_F(ContinueStatementTest, ToStr) { TEST_F(ContinueStatementTest, ToStr) {
auto* stmt = create<ContinueStatement>(); auto* stmt = create<ContinueStatement>();
std::ostringstream out; std::ostringstream out;
stmt->to_str(out, 2); stmt->to_str(Sem(), out, 2);
EXPECT_EQ(out.str(), R"( Continue{} EXPECT_EQ(out.str(), R"( Continue{}
)"); )");
} }

View File

@ -36,7 +36,9 @@ bool DiscardStatement::IsValid() const {
return true; return true;
} }
void DiscardStatement::to_str(std::ostream& out, size_t indent) const { void DiscardStatement::to_str(const semantic::Info&,
std::ostream& out,
size_t indent) const {
make_indent(out, indent); make_indent(out, indent);
out << "Discard{}" << std::endl; out << "Discard{}" << std::endl;
} }

View File

@ -42,9 +42,12 @@ class DiscardStatement : public Castable<DiscardStatement, Statement> {
bool IsValid() const override; bool IsValid() const override;
/// Writes a representation of the node to the output stream /// Writes a representation of the node to the output stream
/// @param sem the semantic info for the program
/// @param out the stream to write to /// @param out the stream to write to
/// @param indent number of spaces to indent the node when writing /// @param indent number of spaces to indent the node when writing
void to_str(std::ostream& out, size_t indent) const override; void to_str(const semantic::Info& sem,
std::ostream& out,
size_t indent) const override;
private: private:
DiscardStatement(const DiscardStatement&) = delete; DiscardStatement(const DiscardStatement&) = delete;

View File

@ -54,7 +54,7 @@ TEST_F(DiscardStatementTest, IsValid) {
TEST_F(DiscardStatementTest, ToStr) { TEST_F(DiscardStatementTest, ToStr) {
auto* stmt = create<DiscardStatement>(); auto* stmt = create<DiscardStatement>();
std::ostringstream out; std::ostringstream out;
stmt->to_str(out, 2); stmt->to_str(Sem(), out, 2);
EXPECT_EQ(out.str(), R"( Discard{} EXPECT_EQ(out.str(), R"( Discard{}
)"); )");
} }

View File

@ -43,14 +43,16 @@ bool ElseStatement::IsValid() const {
return condition_ == nullptr || condition_->IsValid(); return condition_ == nullptr || condition_->IsValid();
} }
void ElseStatement::to_str(std::ostream& out, size_t indent) const { void ElseStatement::to_str(const semantic::Info& sem,
std::ostream& out,
size_t indent) const {
make_indent(out, indent); make_indent(out, indent);
out << "Else{" << std::endl; out << "Else{" << std::endl;
if (condition_ != nullptr) { if (condition_ != nullptr) {
make_indent(out, indent + 2); make_indent(out, indent + 2);
out << "(" << std::endl; out << "(" << std::endl;
condition_->to_str(out, indent + 4); condition_->to_str(sem, out, indent + 4);
make_indent(out, indent + 2); make_indent(out, indent + 2);
out << ")" << std::endl; out << ")" << std::endl;
@ -61,7 +63,7 @@ void ElseStatement::to_str(std::ostream& out, size_t indent) const {
if (body_ != nullptr) { if (body_ != nullptr) {
for (auto* stmt : *body_) { for (auto* stmt : *body_) {
stmt->to_str(out, indent + 4); stmt->to_str(sem, out, indent + 4);
} }
} }

View File

@ -62,9 +62,12 @@ class ElseStatement : public Castable<ElseStatement, Statement> {
bool IsValid() const override; bool IsValid() const override;
/// Writes a representation of the node to the output stream /// Writes a representation of the node to the output stream
/// @param sem the semantic info for the program
/// @param out the stream to write to /// @param out the stream to write to
/// @param indent number of spaces to indent the node when writing /// @param indent number of spaces to indent the node when writing
void to_str(std::ostream& out, size_t indent) const override; void to_str(const semantic::Info& sem,
std::ostream& out,
size_t indent) const override;
private: private:
ElseStatement(const ElseStatement&) = delete; ElseStatement(const ElseStatement&) = delete;

View File

@ -115,7 +115,7 @@ TEST_F(ElseStatementTest, ToStr) {
}); });
auto* e = create<ElseStatement>(cond, body); auto* e = create<ElseStatement>(cond, body);
std::ostringstream out; std::ostringstream out;
e->to_str(out, 2); e->to_str(Sem(), out, 2);
EXPECT_EQ(out.str(), R"( Else{ EXPECT_EQ(out.str(), R"( Else{
( (
ScalarConstructor[not set]{true} ScalarConstructor[not set]{true}
@ -133,7 +133,7 @@ TEST_F(ElseStatementTest, ToStr_NoCondition) {
}); });
auto* e = create<ElseStatement>(nullptr, body); auto* e = create<ElseStatement>(nullptr, body);
std::ostringstream out; std::ostringstream out;
e->to_str(out, 2); e->to_str(Sem(), out, 2);
EXPECT_EQ(out.str(), R"( Else{ EXPECT_EQ(out.str(), R"( Else{
{ {
Discard{} Discard{}

View File

@ -38,7 +38,7 @@ class Expression : public Castable<Expression, Node> {
/// @returns a string representation of the result type or 'not set' if no /// @returns a string representation of the result type or 'not set' if no
/// result type present /// result type present
std::string result_type_str() const { std::string result_type_str(const semantic::Info&) const {
return result_type_ ? result_type_->type_name() : "not set"; return result_type_ ? result_type_->type_name() : "not set";
} }

View File

@ -1,55 +0,0 @@
// Copyright 2020 The Tint Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "src/ast/expression.h"
#include "src/ast/test_helper.h"
#include "src/type/alias_type.h"
#include "src/type/i32_type.h"
namespace tint {
namespace ast {
namespace {
class FakeExpr : public Expression {
public:
FakeExpr() : Expression(Source{}) {}
FakeExpr* Clone(CloneContext*) const override { return nullptr; }
bool IsValid() const override { return true; }
void to_str(std::ostream&, size_t) const override {}
};
using ExpressionTest = TestHelper;
TEST_F(ExpressionTest, set_result_type) {
FakeExpr e;
e.set_result_type(ty.i32());
ASSERT_NE(e.result_type(), nullptr);
EXPECT_TRUE(e.result_type()->Is<type::I32>());
}
TEST_F(ExpressionTest, set_result_type_alias) {
auto* a = ty.alias("a", ty.i32());
auto* b = ty.alias("b", a);
FakeExpr e;
e.set_result_type(b);
ASSERT_NE(e.result_type(), nullptr);
EXPECT_TRUE(e.result_type()->Is<type::I32>());
}
} // namespace
} // namespace ast
} // namespace tint

View File

@ -37,7 +37,9 @@ bool FallthroughStatement::IsValid() const {
return true; return true;
} }
void FallthroughStatement::to_str(std::ostream& out, size_t indent) const { void FallthroughStatement::to_str(const semantic::Info&,
std::ostream& out,
size_t indent) const {
make_indent(out, indent); make_indent(out, indent);
out << "Fallthrough{}" << std::endl; out << "Fallthrough{}" << std::endl;
} }

View File

@ -42,9 +42,12 @@ class FallthroughStatement : public Castable<FallthroughStatement, Statement> {
bool IsValid() const override; bool IsValid() const override;
/// Writes a representation of the node to the output stream /// Writes a representation of the node to the output stream
/// @param sem the semantic info for the program
/// @param out the stream to write to /// @param out the stream to write to
/// @param indent number of spaces to indent the node when writing /// @param indent number of spaces to indent the node when writing
void to_str(std::ostream& out, size_t indent) const override; void to_str(const semantic::Info& sem,
std::ostream& out,
size_t indent) const override;
private: private:
FallthroughStatement(const FallthroughStatement&) = delete; FallthroughStatement(const FallthroughStatement&) = delete;

View File

@ -50,7 +50,7 @@ TEST_F(FallthroughStatementTest, IsValid) {
TEST_F(FallthroughStatementTest, ToStr) { TEST_F(FallthroughStatementTest, ToStr) {
auto* stmt = create<FallthroughStatement>(); auto* stmt = create<FallthroughStatement>();
std::ostringstream out; std::ostringstream out;
stmt->to_str(out, 2); stmt->to_str(Sem(), out, 2);
EXPECT_EQ(out.str(), R"( Fallthrough{} EXPECT_EQ(out.str(), R"( Fallthrough{}
)"); )");
} }

View File

@ -30,7 +30,7 @@ FloatLiteral::FloatLiteral(const Source& source, type::Type* type, float value)
FloatLiteral::~FloatLiteral() = default; FloatLiteral::~FloatLiteral() = default;
std::string FloatLiteral::to_str() const { std::string FloatLiteral::to_str(const semantic::Info&) const {
return std::to_string(value_); return std::to_string(value_);
} }

View File

@ -38,8 +38,9 @@ class FloatLiteral : public Castable<FloatLiteral, Literal> {
/// @returns the name for this literal. This name is unique to this value. /// @returns the name for this literal. This name is unique to this value.
std::string name() const override; std::string name() const override;
/// @param sem the semantic info for the program
/// @returns the literal as a string /// @returns the literal as a string
std::string to_str() const override; std::string to_str(const semantic::Info& sem) const override;
/// Clones this node and all transitive child nodes using the `CloneContext` /// Clones this node and all transitive child nodes using the `CloneContext`
/// `ctx`. /// `ctx`.

View File

@ -46,7 +46,7 @@ TEST_F(FloatLiteralTest, Is) {
TEST_F(FloatLiteralTest, ToStr) { TEST_F(FloatLiteralTest, ToStr) {
auto* f = create<FloatLiteral>(ty.f32(), 42.1f); auto* f = create<FloatLiteral>(ty.f32(), 42.1f);
EXPECT_EQ(f->to_str(), "42.099998"); EXPECT_EQ(f->to_str(Sem()), "42.099998");
} }
TEST_F(FloatLiteralTest, ToName) { TEST_F(FloatLiteralTest, ToName) {

View File

@ -247,13 +247,15 @@ bool Function::IsValid() const {
return true; return true;
} }
void Function::to_str(std::ostream& out, size_t indent) const { void Function::to_str(const semantic::Info& sem,
std::ostream& out,
size_t indent) const {
make_indent(out, indent); make_indent(out, indent);
out << "Function " << symbol_.to_str() << " -> " << return_type_->type_name() out << "Function " << symbol_.to_str() << " -> " << return_type_->type_name()
<< std::endl; << std::endl;
for (auto* deco : decorations()) { for (auto* deco : decorations()) {
deco->to_str(out, indent); deco->to_str(sem, out, indent);
} }
make_indent(out, indent); make_indent(out, indent);
@ -263,7 +265,7 @@ void Function::to_str(std::ostream& out, size_t indent) const {
out << std::endl; out << std::endl;
for (auto* param : params_) for (auto* param : params_)
param->to_str(out, indent + 2); param->to_str(sem, out, indent + 2);
make_indent(out, indent); make_indent(out, indent);
} }
@ -274,7 +276,7 @@ void Function::to_str(std::ostream& out, size_t indent) const {
if (body_ != nullptr) { if (body_ != nullptr) {
for (auto* stmt : *body_) { for (auto* stmt : *body_) {
stmt->to_str(out, indent + 2); stmt->to_str(sem, out, indent + 2);
} }
} }

View File

@ -184,9 +184,12 @@ class Function : public Castable<Function, Node> {
bool IsValid() const override; bool IsValid() const override;
/// Writes a representation of the node to the output stream /// Writes a representation of the node to the output stream
/// @param sem the semantic info for the program
/// @param out the stream to write to /// @param out the stream to write to
/// @param indent number of spaces to indent the node when writing /// @param indent number of spaces to indent the node when writing
void to_str(std::ostream& out, size_t indent) const override; void to_str(const semantic::Info& sem,
std::ostream& out,
size_t indent) const override;
/// @returns the type name for this function /// @returns the type name for this function
std::string type_name() const; std::string type_name() const;

View File

@ -245,7 +245,7 @@ TEST_F(FunctionTest, ToStr) {
FunctionDecorationList{}); FunctionDecorationList{});
std::ostringstream out; std::ostringstream out;
f->to_str(out, 2); f->to_str(Sem(), out, 2);
EXPECT_EQ(demangle(out.str()), R"( Function func -> __void EXPECT_EQ(demangle(out.str()), R"( Function func -> __void
() ()
{ {
@ -262,7 +262,7 @@ TEST_F(FunctionTest, ToStr_WithDecoration) {
FunctionDecorationList{create<WorkgroupDecoration>(2, 4, 6)}); FunctionDecorationList{create<WorkgroupDecoration>(2, 4, 6)});
std::ostringstream out; std::ostringstream out;
f->to_str(out, 2); f->to_str(Sem(), out, 2);
EXPECT_EQ(demangle(out.str()), R"( Function func -> __void EXPECT_EQ(demangle(out.str()), R"( Function func -> __void
WorkgroupDecoration{2 4 6} WorkgroupDecoration{2 4 6}
() ()
@ -283,7 +283,7 @@ TEST_F(FunctionTest, ToStr_WithParams) {
FunctionDecorationList{}); FunctionDecorationList{});
std::ostringstream out; std::ostringstream out;
f->to_str(out, 2); f->to_str(Sem(), out, 2);
EXPECT_EQ(demangle(out.str()), R"( Function func -> __void EXPECT_EQ(demangle(out.str()), R"( Function func -> __void
( (
Variable{ Variable{

View File

@ -27,7 +27,9 @@ GroupDecoration::GroupDecoration(const Source& source, uint32_t val)
GroupDecoration::~GroupDecoration() = default; GroupDecoration::~GroupDecoration() = default;
void GroupDecoration::to_str(std::ostream& out, size_t indent) const { void GroupDecoration::to_str(const semantic::Info&,
std::ostream& out,
size_t indent) const {
make_indent(out, indent); make_indent(out, indent);
out << "GroupDecoration{" << value_ << "}" << std::endl; out << "GroupDecoration{" << value_ << "}" << std::endl;
} }

View File

@ -35,9 +35,12 @@ class GroupDecoration : public Castable<GroupDecoration, 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 sem the semantic info for the program
/// @param out the stream to write to /// @param out the stream to write to
/// @param indent number of spaces to indent the node when writing /// @param indent number of spaces to indent the node when writing
void to_str(std::ostream& out, size_t indent) const override; void to_str(const semantic::Info& sem,
std::ostream& out,
size_t indent) const override;
/// Clones this node and all transitive child nodes using the `CloneContext` /// Clones this node and all transitive child nodes using the `CloneContext`
/// `ctx`. /// `ctx`.

View File

@ -40,7 +40,7 @@ TEST_F(GroupDecorationTest, Is) {
TEST_F(GroupDecorationTest, ToStr) { TEST_F(GroupDecorationTest, ToStr) {
auto* d = create<GroupDecoration>(2); auto* d = create<GroupDecoration>(2);
std::ostringstream out; std::ostringstream out;
d->to_str(out, 0); d->to_str(Sem(), out, 0);
EXPECT_EQ(out.str(), R"(GroupDecoration{2} EXPECT_EQ(out.str(), R"(GroupDecoration{2}
)"); )");
} }

View File

@ -38,9 +38,11 @@ bool IdentifierExpression::IsValid() const {
return sym_.IsValid(); return sym_.IsValid();
} }
void IdentifierExpression::to_str(std::ostream& out, size_t indent) const { void IdentifierExpression::to_str(const semantic::Info& sem,
std::ostream& out,
size_t indent) const {
make_indent(out, indent); make_indent(out, indent);
out << "Identifier[" << result_type_str() << "]{" << sym_.to_str() << "}" out << "Identifier[" << result_type_str(sem) << "]{" << sym_.to_str() << "}"
<< std::endl; << std::endl;
} }

View File

@ -76,9 +76,12 @@ class IdentifierExpression : public Castable<IdentifierExpression, Expression> {
bool IsValid() const override; bool IsValid() const override;
/// Writes a representation of the node to the output stream /// Writes a representation of the node to the output stream
/// @param sem the semantic info for the program
/// @param out the stream to write to /// @param out the stream to write to
/// @param indent number of spaces to indent the node when writing /// @param indent number of spaces to indent the node when writing
void to_str(std::ostream& out, size_t indent) const override; void to_str(const semantic::Info& sem,
std::ostream& out,
size_t indent) const override;
private: private:
IdentifierExpression(const IdentifierExpression&) = delete; IdentifierExpression(const IdentifierExpression&) = delete;

View File

@ -49,7 +49,7 @@ TEST_F(IdentifierExpressionTest, IsValid) {
TEST_F(IdentifierExpressionTest, ToStr) { TEST_F(IdentifierExpressionTest, ToStr) {
auto* i = Expr("ident"); auto* i = Expr("ident");
std::ostringstream out; std::ostringstream out;
i->to_str(out, 2); i->to_str(Sem(), out, 2);
EXPECT_EQ(demangle(out.str()), R"( Identifier[not set]{ident} EXPECT_EQ(demangle(out.str()), R"( Identifier[not set]{ident}
)"); )");
} }

View File

@ -67,7 +67,9 @@ bool IfStatement::IsValid() const {
return true; return true;
} }
void IfStatement::to_str(std::ostream& out, size_t indent) const { void IfStatement::to_str(const semantic::Info& sem,
std::ostream& out,
size_t indent) const {
make_indent(out, indent); make_indent(out, indent);
out << "If{" << std::endl; out << "If{" << std::endl;
@ -75,7 +77,7 @@ void IfStatement::to_str(std::ostream& out, size_t indent) const {
make_indent(out, indent + 2); make_indent(out, indent + 2);
out << "(" << std::endl; out << "(" << std::endl;
condition_->to_str(out, indent + 4); condition_->to_str(sem, out, indent + 4);
// Close if conditional // Close if conditional
make_indent(out, indent + 2); make_indent(out, indent + 2);
@ -87,7 +89,7 @@ void IfStatement::to_str(std::ostream& out, size_t indent) const {
if (body_ != nullptr) { if (body_ != nullptr) {
for (auto* stmt : *body_) { for (auto* stmt : *body_) {
stmt->to_str(out, indent + 4); stmt->to_str(sem, out, indent + 4);
} }
} }
@ -100,7 +102,7 @@ void IfStatement::to_str(std::ostream& out, size_t indent) const {
out << "}" << std::endl; out << "}" << std::endl;
for (auto* e : else_statements_) { for (auto* e : else_statements_) {
e->to_str(out, indent); e->to_str(sem, out, indent);
} }
} }

View File

@ -67,9 +67,12 @@ class IfStatement : public Castable<IfStatement, Statement> {
bool IsValid() const override; bool IsValid() const override;
/// Writes a representation of the node to the output stream /// Writes a representation of the node to the output stream
/// @param sem the semantic info for the program
/// @param out the stream to write to /// @param out the stream to write to
/// @param indent number of spaces to indent the node when writing /// @param indent number of spaces to indent the node when writing
void to_str(std::ostream& out, size_t indent) const override; void to_str(const semantic::Info& sem,
std::ostream& out,
size_t indent) const override;
private: private:
IfStatement(const IfStatement&) = delete; IfStatement(const IfStatement&) = delete;

View File

@ -168,7 +168,7 @@ TEST_F(IfStatementTest, ToStr) {
auto* stmt = create<IfStatement>(cond, body, ElseStatementList{}); auto* stmt = create<IfStatement>(cond, body, ElseStatementList{});
std::ostringstream out; std::ostringstream out;
stmt->to_str(out, 2); stmt->to_str(Sem(), out, 2);
EXPECT_EQ(demangle(out.str()), R"( If{ EXPECT_EQ(demangle(out.str()), R"( If{
( (
Identifier[not set]{cond} Identifier[not set]{cond}
@ -196,7 +196,7 @@ TEST_F(IfStatementTest, ToStr_WithElseStatements) {
}); });
std::ostringstream out; std::ostringstream out;
stmt->to_str(out, 2); stmt->to_str(Sem(), out, 2);
EXPECT_EQ(demangle(out.str()), R"( If{ EXPECT_EQ(demangle(out.str()), R"( If{
( (
Identifier[not set]{cond} Identifier[not set]{cond}

View File

@ -28,9 +28,11 @@ bool Literal::IsValid() const {
return true; return true;
} }
void Literal::to_str(std::ostream& out, size_t indent) const { void Literal::to_str(const semantic::Info& sem,
std::ostream& out,
size_t indent) const {
make_indent(out, indent); make_indent(out, indent);
out << to_str(); out << to_str(sem);
} }
} // namespace ast } // namespace ast

View File

@ -35,12 +35,16 @@ class Literal : public Castable<Literal, Node> {
bool IsValid() const override; bool IsValid() const override;
/// Writes a representation of the node to the output stream /// Writes a representation of the node to the output stream
/// @param sem the semantic info for the program
/// @param out the stream to write to /// @param out the stream to write to
/// @param indent number of spaces to indent the node when writing /// @param indent number of spaces to indent the node when writing
void to_str(std::ostream& out, size_t indent) const override; void to_str(const semantic::Info& sem,
std::ostream& out,
size_t indent) const override;
/// @param sem the semantic info for the program
/// @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 semantic::Info& sem) const = 0;
/// @returns the name for this literal. This name is unique to this value. /// @returns the name for this literal. This name is unique to this value.
virtual std::string name() const = 0; virtual std::string name() const = 0;

View File

@ -27,7 +27,9 @@ LocationDecoration::LocationDecoration(const Source& source, uint32_t val)
LocationDecoration::~LocationDecoration() = default; LocationDecoration::~LocationDecoration() = default;
void LocationDecoration::to_str(std::ostream& out, size_t indent) const { void LocationDecoration::to_str(const semantic::Info&,
std::ostream& out,
size_t indent) const {
make_indent(out, indent); make_indent(out, indent);
out << "LocationDecoration{" << value_ << "}" << std::endl; out << "LocationDecoration{" << value_ << "}" << std::endl;
} }

View File

@ -36,9 +36,12 @@ class LocationDecoration
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 sem the semantic info for the program
/// @param out the stream to write to /// @param out the stream to write to
/// @param indent number of spaces to indent the node when writing /// @param indent number of spaces to indent the node when writing
void to_str(std::ostream& out, size_t indent) const override; void to_str(const semantic::Info& sem,
std::ostream& out,
size_t indent) const override;
/// Clones this node and all transitive child nodes using the `CloneContext` /// Clones this node and all transitive child nodes using the `CloneContext`
/// `ctx`. /// `ctx`.

View File

@ -42,7 +42,7 @@ TEST_F(LocationDecorationTest, Is) {
TEST_F(LocationDecorationTest, ToStr) { TEST_F(LocationDecorationTest, ToStr) {
auto* d = create<LocationDecoration>(2); auto* d = create<LocationDecoration>(2);
std::ostringstream out; std::ostringstream out;
d->to_str(out, 0); d->to_str(Sem(), out, 0);
EXPECT_EQ(out.str(), R"(LocationDecoration{2} EXPECT_EQ(out.str(), R"(LocationDecoration{2}
)"); )");
} }

View File

@ -46,13 +46,15 @@ bool LoopStatement::IsValid() const {
return true; return true;
} }
void LoopStatement::to_str(std::ostream& out, size_t indent) const { void LoopStatement::to_str(const semantic::Info& sem,
std::ostream& out,
size_t indent) const {
make_indent(out, indent); make_indent(out, indent);
out << "Loop{" << std::endl; out << "Loop{" << std::endl;
if (body_ != nullptr) { if (body_ != nullptr) {
for (auto* stmt : *body_) { for (auto* stmt : *body_) {
stmt->to_str(out, indent + 2); stmt->to_str(sem, out, indent + 2);
} }
} }
@ -61,7 +63,7 @@ void LoopStatement::to_str(std::ostream& out, size_t indent) const {
out << "continuing {" << std::endl; out << "continuing {" << std::endl;
for (auto* stmt : *continuing_) { for (auto* stmt : *continuing_) {
stmt->to_str(out, indent + 4); stmt->to_str(sem, out, indent + 4);
} }
make_indent(out, indent + 2); make_indent(out, indent + 2);

View File

@ -64,9 +64,12 @@ class LoopStatement : public Castable<LoopStatement, Statement> {
bool IsValid() const override; bool IsValid() const override;
/// Writes a representation of the node to the output stream /// Writes a representation of the node to the output stream
/// @param sem the semantic info for the program
/// @param out the stream to write to /// @param out the stream to write to
/// @param indent number of spaces to indent the node when writing /// @param indent number of spaces to indent the node when writing
void to_str(std::ostream& out, size_t indent) const override; void to_str(const semantic::Info& sem,
std::ostream& out,
size_t indent) const override;
private: private:
LoopStatement(const LoopStatement&) = delete; LoopStatement(const LoopStatement&) = delete;

View File

@ -171,7 +171,7 @@ TEST_F(LoopStatementTest, ToStr) {
auto* l = create<LoopStatement>(body, nullptr); auto* l = create<LoopStatement>(body, nullptr);
std::ostringstream out; std::ostringstream out;
l->to_str(out, 2); l->to_str(Sem(), out, 2);
EXPECT_EQ(out.str(), R"( Loop{ EXPECT_EQ(out.str(), R"( Loop{
Discard{} Discard{}
} }
@ -187,7 +187,7 @@ TEST_F(LoopStatementTest, ToStr_WithContinuing) {
auto* l = create<LoopStatement>(body, continuing); auto* l = create<LoopStatement>(body, continuing);
std::ostringstream out; std::ostringstream out;
l->to_str(out, 2); l->to_str(Sem(), out, 2);
EXPECT_EQ(out.str(), R"( Loop{ EXPECT_EQ(out.str(), R"( Loop{
Discard{} Discard{}
continuing { continuing {

View File

@ -48,11 +48,13 @@ bool MemberAccessorExpression::IsValid() const {
return true; return true;
} }
void MemberAccessorExpression::to_str(std::ostream& out, size_t indent) const { void MemberAccessorExpression::to_str(const semantic::Info& sem,
std::ostream& out,
size_t indent) const {
make_indent(out, indent); make_indent(out, indent);
out << "MemberAccessor[" << result_type_str() << "]{" << std::endl; out << "MemberAccessor[" << result_type_str(sem) << "]{" << std::endl;
struct_->to_str(out, indent + 2); struct_->to_str(sem, out, indent + 2);
member_->to_str(out, indent + 2); member_->to_str(sem, out, indent + 2);
make_indent(out, indent); make_indent(out, indent);
out << "}" << std::endl; out << "}" << std::endl;
} }

View File

@ -57,9 +57,12 @@ class MemberAccessorExpression
bool IsValid() const override; bool IsValid() const override;
/// Writes a representation of the node to the output stream /// Writes a representation of the node to the output stream
/// @param sem the semantic info for the program
/// @param out the stream to write to /// @param out the stream to write to
/// @param indent number of spaces to indent the node when writing /// @param indent number of spaces to indent the node when writing
void to_str(std::ostream& out, size_t indent) const override; void to_str(const semantic::Info& sem,
std::ostream& out,
size_t indent) const override;
private: private:
MemberAccessorExpression(const MemberAccessorExpression&) = delete; MemberAccessorExpression(const MemberAccessorExpression&) = delete;

View File

@ -78,7 +78,7 @@ TEST_F(MemberAccessorExpressionTest, ToStr) {
auto* stmt = auto* stmt =
create<MemberAccessorExpression>(Expr("structure"), Expr("member")); create<MemberAccessorExpression>(Expr("structure"), Expr("member"));
std::ostringstream out; std::ostringstream out;
stmt->to_str(out, 2); stmt->to_str(Sem(), out, 2);
EXPECT_EQ(demangle(out.str()), R"( MemberAccessor[not set]{ EXPECT_EQ(demangle(out.str()), R"( MemberAccessor[not set]{
Identifier[not set]{structure} Identifier[not set]{structure}
Identifier[not set]{member} Identifier[not set]{member}

View File

@ -81,7 +81,9 @@ Module* Module::Clone(CloneContext* ctx) const {
ctx->Clone(global_variables_)); ctx->Clone(global_variables_));
} }
void Module::to_str(std::ostream& out, size_t indent) const { void Module::to_str(const semantic::Info& sem,
std::ostream& out,
size_t indent) const {
make_indent(out, indent); make_indent(out, indent);
out << "Module{" << std::endl; out << "Module{" << std::endl;
indent += 2; indent += 2;
@ -91,25 +93,25 @@ void Module::to_str(std::ostream& out, size_t indent) const {
out << alias->symbol().to_str() << " -> " << alias->type()->type_name() out << alias->symbol().to_str() << " -> " << alias->type()->type_name()
<< std::endl; << std::endl;
if (auto* str = alias->type()->As<type::Struct>()) { if (auto* str = alias->type()->As<type::Struct>()) {
str->impl()->to_str(out, indent); str->impl()->to_str(sem, out, indent);
} }
} else if (auto* str = ty->As<type::Struct>()) { } else if (auto* str = ty->As<type::Struct>()) {
out << str->symbol().to_str() << " "; out << str->symbol().to_str() << " ";
str->impl()->to_str(out, indent); str->impl()->to_str(sem, out, indent);
} }
} }
for (auto* var : global_variables_) { for (auto* var : global_variables_) {
var->to_str(out, indent); var->to_str(sem, out, indent);
} }
for (auto* func : functions_) { for (auto* func : functions_) {
func->to_str(out, indent); func->to_str(sem, out, indent);
} }
out << "}" << std::endl; out << "}" << std::endl;
} }
std::string Module::to_str() const { std::string Module::to_str(const semantic::Info& sem) const {
std::ostringstream out; std::ostringstream out;
to_str(out, 0); to_str(sem, out, 0);
return out.str(); return out.str();
} }

View File

@ -89,12 +89,16 @@ class Module : public Castable<Module, Node> {
Module* Clone(CloneContext* ctx) const override; Module* Clone(CloneContext* ctx) const override;
/// Writes a representation of the node to the output stream /// Writes a representation of the node to the output stream
/// @param sem the semantic info for the program
/// @param out the stream to write to /// @param out the stream to write to
/// @param indent number of spaces to indent the node when writing /// @param indent number of spaces to indent the node when writing
void to_str(std::ostream& out, size_t indent) const override; void to_str(const semantic::Info& sem,
std::ostream& out,
size_t indent) const override;
/// @param sem the semantic info for the program
/// @returns a string representation of the Builder /// @returns a string representation of the Builder
std::string to_str() const; std::string to_str(const semantic::Info& sem) const;
private: private:
std::vector<type::Type*> constructed_types_; std::vector<type::Type*> constructed_types_;

View File

@ -122,8 +122,8 @@ fn main() -> void {
// Expect the AST printed with to_str() to match // Expect the AST printed with to_str() to match
Demangler demanger; Demangler demanger;
EXPECT_EQ(demanger.Demangle(src.Symbols(), src.AST().to_str()), EXPECT_EQ(demanger.Demangle(src.Symbols(), src.AST().to_str(src.Sem())),
demanger.Demangle(dst.Symbols(), dst.AST().to_str())); demanger.Demangle(dst.Symbols(), dst.AST().to_str(dst.Sem())));
// Check that none of the AST nodes or type pointers in dst are found in src // Check that none of the AST nodes or type pointers in dst are found in src
std::unordered_set<ast::Node*> src_nodes; std::unordered_set<ast::Node*> src_nodes;
@ -135,7 +135,7 @@ fn main() -> void {
src_types.emplace(src_type); src_types.emplace(src_type);
} }
for (auto* dst_node : dst.Nodes().Objects()) { for (auto* dst_node : dst.Nodes().Objects()) {
ASSERT_EQ(src_nodes.count(dst_node), 0u) << dst_node->str(); ASSERT_EQ(src_nodes.count(dst_node), 0u) << dst_node->str(dst.Sem());
} }
for (auto* dst_type : dst.Types()) { for (auto* dst_type : dst.Types()) {
ASSERT_EQ(src_types.count(dst_type), 0u) << dst_type->type_name(); ASSERT_EQ(src_types.count(dst_type), 0u) << dst_type->type_name();

View File

@ -36,7 +36,7 @@ TEST_F(ModuleTest, Creation) {
} }
TEST_F(ModuleTest, ToStrEmitsPreambleAndPostamble) { TEST_F(ModuleTest, ToStrEmitsPreambleAndPostamble) {
const auto str = Program(std::move(*this)).AST().to_str(); const auto str = Program(std::move(*this)).to_str();
auto* const expected = "Module{\n}\n"; auto* const expected = "Module{\n}\n";
EXPECT_EQ(str, expected); EXPECT_EQ(str, expected);
} }

View File

@ -32,9 +32,9 @@ void Node::make_indent(std::ostream& out, size_t indent) const {
out << " "; out << " ";
} }
std::string Node::str() const { std::string Node::str(const semantic::Info& sem) const {
std::ostringstream out; std::ostringstream out;
to_str(out, 0); to_str(sem, out, 0);
return out.str(); return out.str();
} }

View File

@ -29,6 +29,9 @@ class CloneContext;
namespace type { namespace type {
class Type; class Type;
} }
namespace semantic {
class Info;
}
namespace ast { namespace ast {
@ -52,13 +55,17 @@ class Node : public Castable<Node> {
virtual bool IsValid() const = 0; virtual bool IsValid() const = 0;
/// Writes a representation of the node to the output stream /// Writes a representation of the node to the output stream
/// @param sem the semantic info for the program
/// @param out the stream to write to /// @param out the stream to write to
/// @param indent number of spaces to indent the node when writing /// @param indent number of spaces to indent the node when writing
virtual void to_str(std::ostream& out, size_t indent) const = 0; virtual void to_str(const semantic::Info& sem,
std::ostream& out,
size_t indent) const = 0;
/// Convenience wrapper around the to_str() method. /// Convenience wrapper around the to_str() method.
/// @param sem the semantic info for the program
/// @returns the node as a string /// @returns the node as a string
std::string str() const; std::string str(const semantic::Info& sem) const;
protected: protected:
/// Create a new node /// Create a new node

View File

@ -27,7 +27,7 @@ NullLiteral::NullLiteral(const Source& source, type::Type* type)
NullLiteral::~NullLiteral() = default; NullLiteral::~NullLiteral() = default;
std::string NullLiteral::to_str() const { std::string NullLiteral::to_str(const semantic::Info&) const {
return "null " + type()->type_name(); return "null " + type()->type_name();
} }

View File

@ -34,8 +34,9 @@ class NullLiteral : public Castable<NullLiteral, Literal> {
/// @returns the name for this literal. This name is unique to this value. /// @returns the name for this literal. This name is unique to this value.
std::string name() const override; std::string name() const override;
/// @param sem the semantic info for the program
/// @returns the literal as a string /// @returns the literal as a string
std::string to_str() const override; std::string to_str(const semantic::Info& sem) const override;
/// Clones this node and all transitive child nodes using the `CloneContext` /// Clones this node and all transitive child nodes using the `CloneContext`
/// `ctx`. /// `ctx`.

View File

@ -40,7 +40,7 @@ TEST_F(NullLiteralTest, Is) {
TEST_F(NullLiteralTest, ToStr) { TEST_F(NullLiteralTest, ToStr) {
auto* i = create<NullLiteral>(ty.i32()); auto* i = create<NullLiteral>(ty.i32());
EXPECT_EQ(i->to_str(), "null __i32"); EXPECT_EQ(i->to_str(Sem()), "null __i32");
} }
TEST_F(NullLiteralTest, Name_I32) { TEST_F(NullLiteralTest, Name_I32) {

View File

@ -44,7 +44,9 @@ bool ReturnStatement::IsValid() const {
return true; return true;
} }
void ReturnStatement::to_str(std::ostream& out, size_t indent) const { void ReturnStatement::to_str(const semantic::Info& sem,
std::ostream& out,
size_t indent) const {
make_indent(out, indent); make_indent(out, indent);
out << "Return{"; out << "Return{";
@ -54,7 +56,7 @@ void ReturnStatement::to_str(std::ostream& out, size_t indent) const {
make_indent(out, indent + 2); make_indent(out, indent + 2);
out << "{" << std::endl; out << "{" << std::endl;
value_->to_str(out, indent + 4); value_->to_str(sem, out, indent + 4);
make_indent(out, indent + 2); make_indent(out, indent + 2);
out << "}" << std::endl; out << "}" << std::endl;

View File

@ -55,9 +55,12 @@ class ReturnStatement : public Castable<ReturnStatement, Statement> {
bool IsValid() const override; bool IsValid() const override;
/// Writes a representation of the node to the output stream /// Writes a representation of the node to the output stream
/// @param sem the semantic info for the program
/// @param out the stream to write to /// @param out the stream to write to
/// @param indent number of spaces to indent the node when writing /// @param indent number of spaces to indent the node when writing
void to_str(std::ostream& out, size_t indent) const override; void to_str(const semantic::Info& sem,
std::ostream& out,
size_t indent) const override;
private: private:
ReturnStatement(const ReturnStatement&) = delete; ReturnStatement(const ReturnStatement&) = delete;

View File

@ -76,7 +76,7 @@ TEST_F(ReturnStatementTest, ToStr_WithValue) {
auto* expr = Expr("expr"); auto* expr = Expr("expr");
auto* r = create<ReturnStatement>(expr); auto* r = create<ReturnStatement>(expr);
std::ostringstream out; std::ostringstream out;
r->to_str(out, 2); r->to_str(Sem(), out, 2);
EXPECT_EQ(demangle(out.str()), R"( Return{ EXPECT_EQ(demangle(out.str()), R"( Return{
{ {
Identifier[not set]{expr} Identifier[not set]{expr}
@ -88,7 +88,7 @@ TEST_F(ReturnStatementTest, ToStr_WithValue) {
TEST_F(ReturnStatementTest, ToStr_WithoutValue) { TEST_F(ReturnStatementTest, ToStr_WithoutValue) {
auto* r = create<ReturnStatement>(); auto* r = create<ReturnStatement>();
std::ostringstream out; std::ostringstream out;
r->to_str(out, 2); r->to_str(Sem(), out, 2);
EXPECT_EQ(out.str(), R"( Return{} EXPECT_EQ(out.str(), R"( Return{}
)"); )");
} }

View File

@ -41,11 +41,12 @@ bool ScalarConstructorExpression::IsValid() const {
return literal_ != nullptr; return literal_ != nullptr;
} }
void ScalarConstructorExpression::to_str(std::ostream& out, void ScalarConstructorExpression::to_str(const semantic::Info& sem,
std::ostream& out,
size_t indent) const { size_t indent) const {
make_indent(out, indent); make_indent(out, indent);
out << "ScalarConstructor[" << result_type_str() << "]{" << literal_->to_str() out << "ScalarConstructor[" << result_type_str(sem) << "]{"
<< "}" << std::endl; << literal_->to_str(sem) << "}" << std::endl;
} }
} // namespace ast } // namespace ast

View File

@ -51,9 +51,12 @@ class ScalarConstructorExpression
bool IsValid() const override; bool IsValid() const override;
/// Writes a representation of the node to the output stream /// Writes a representation of the node to the output stream
/// @param sem the semantic info for the program
/// @param out the stream to write to /// @param out the stream to write to
/// @param indent number of spaces to indent the node when writing /// @param indent number of spaces to indent the node when writing
void to_str(std::ostream& out, size_t indent) const override; void to_str(const semantic::Info& sem,
std::ostream& out,
size_t indent) const override;
private: private:
ScalarConstructorExpression(const ScalarConstructorExpression&) = delete; ScalarConstructorExpression(const ScalarConstructorExpression&) = delete;

View File

@ -50,7 +50,7 @@ TEST_F(ScalarConstructorExpressionTest, IsValid_MissingLiteral) {
TEST_F(ScalarConstructorExpressionTest, ToStr) { TEST_F(ScalarConstructorExpressionTest, ToStr) {
auto* c = Expr(true); auto* c = Expr(true);
std::ostringstream out; std::ostringstream out;
c->to_str(out, 2); c->to_str(Sem(), out, 2);
EXPECT_EQ(out.str(), R"( ScalarConstructor[not set]{true} EXPECT_EQ(out.str(), R"( ScalarConstructor[not set]{true}
)"); )");
} }

View File

@ -27,7 +27,7 @@ SintLiteral::SintLiteral(const Source& source, type::Type* type, int32_t value)
SintLiteral::~SintLiteral() = default; SintLiteral::~SintLiteral() = default;
std::string SintLiteral::to_str() const { std::string SintLiteral::to_str(const semantic::Info&) const {
return std::to_string(value_); return std::to_string(value_);
} }

Some files were not shown because too many files have changed in this diff Show More