[ast] Adds get last statement to ast::Function

This CL adds a function which returns the last statement of a ast::Function

Change-Id: I1dc68b7f4669c17a24a62c03a87dcc95866a428d
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/26720
Reviewed-by: dan sinclair <dsinclair@chromium.org>
Commit-Queue: dan sinclair <dsinclair@chromium.org>
This commit is contained in:
Sarah Mashayekhi 2020-08-12 20:06:29 +00:00 committed by Commit Bot service account
parent f16250b8ac
commit 5e7ef27ca7
3 changed files with 31 additions and 0 deletions

View File

@ -156,6 +156,10 @@ void Function::add_ancestor_entry_point(const std::string& ep) {
ancestor_entry_points_.push_back(ep); ancestor_entry_points_.push_back(ep);
} }
const Statement* Function::get_last_statement() const {
return body_->last();
}
bool Function::IsValid() const { bool Function::IsValid() const {
for (const auto& param : params_) { for (const auto& param : params_) {
if (param == nullptr || !param->IsValid()) if (param == nullptr || !param->IsValid())

View File

@ -121,6 +121,9 @@ class Function : public Node {
void set_return_type(type::Type* type) { return_type_ = type; } void set_return_type(type::Type* type) { return_type_ = type; }
/// @returns the function return type. /// @returns the function return type.
type::Type* return_type() const { return return_type_; } type::Type* return_type() const { return return_type_; }
/// @returns a pointer to the last statement of the function or nullptr if
// function is empty
const Statement* get_last_statement() const;
/// Sets the body of the function /// Sets the body of the function
/// @param body the function body /// @param body the function body

View File

@ -349,6 +349,30 @@ TEST_F(FunctionTest, TypeName_WithParams) {
EXPECT_EQ(f.type_name(), "__func__void__i32__f32"); EXPECT_EQ(f.type_name(), "__func__void__i32__f32");
} }
TEST_F(FunctionTest, GetLastStatement) {
type::VoidType void_type;
VariableList params;
auto body = std::make_unique<ast::BlockStatement>();
auto stmt = std::make_unique<DiscardStatement>();
auto* stmt_ptr = stmt.get();
body->append(std::move(stmt));
Function f("func", std::move(params), &void_type);
f.set_body(std::move(body));
EXPECT_EQ(f.get_last_statement(), stmt_ptr);
}
TEST_F(FunctionTest, GetLastStatement_nullptr) {
type::VoidType void_type;
VariableList params;
auto body = std::make_unique<ast::BlockStatement>();
Function f("func", std::move(params), &void_type);
f.set_body(std::move(body));
EXPECT_EQ(f.get_last_statement(), nullptr);
}
} // namespace } // namespace
} // namespace ast } // namespace ast
} // namespace tint } // namespace tint