Add BlockStatement insert and non-const global_variables

Insert will be used for adding code at the beginning of a function, and
non-const global_variables to edit decorations.

Bug: dawn:480
Change-Id: I9fbb0210a95b8488c9ce6d9a536a68f169b0cc33
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/25850
Reviewed-by: dan sinclair <dsinclair@chromium.org>
Reviewed-by: Corentin Wallez <cwallez@chromium.org>
This commit is contained in:
Idan Raiter 2020-07-30 13:59:00 +00:00 committed by dan sinclair
parent a3f9778ee6
commit a8e4e2ad5d
3 changed files with 31 additions and 0 deletions

View File

@ -42,6 +42,13 @@ class BlockStatement : public Statement {
statements_.push_back(std::move(stmt)); statements_.push_back(std::move(stmt));
} }
/// Insert a statement to the block
/// @param index the index to insert at
/// @param stmt the statement to insert
void insert(size_t index, std::unique_ptr<ast::Statement> stmt) {
statements_.insert(statements_.begin() + index, std::move(stmt));
}
/// @returns true if the block is empty /// @returns true if the block is empty
bool empty() const { return statements_.empty(); } bool empty() const { return statements_.empty(); }
/// @returns the number of statements directly in the block /// @returns the number of statements directly in the block

View File

@ -38,6 +38,27 @@ TEST_F(BlockStatementTest, Creation) {
EXPECT_EQ(b[0], ptr); EXPECT_EQ(b[0], ptr);
} }
TEST_F(BlockStatementTest, Creation_WithInsert) {
auto s1 = std::make_unique<DiscardStatement>();
auto s2 = std::make_unique<DiscardStatement>();
auto s3 = std::make_unique<DiscardStatement>();
auto* p1 = s1.get();
auto* p2 = s2.get();
auto* p3 = s3.get();
BlockStatement b;
b.insert(0, std::move(s1));
b.insert(0, std::move(s2));
b.insert(1, std::move(s3));
// |b| should contain s2, s3, s1
ASSERT_EQ(b.size(), 3u);
EXPECT_EQ(b[0], p2);
EXPECT_EQ(b[1], p3);
EXPECT_EQ(b[2], p1);
}
TEST_F(BlockStatementTest, Creation_WithSource) { TEST_F(BlockStatementTest, Creation_WithSource) {
BlockStatement b(Source{20, 2}); BlockStatement b(Source{20, 2});
auto src = b.source(); auto src = b.source();

View File

@ -57,6 +57,9 @@ class Module {
/// @returns the global variables for the module /// @returns the global variables for the module
const VariableList& global_variables() const { return global_variables_; } const VariableList& global_variables() const { return global_variables_; }
/// @returns the global variables for the module
VariableList& global_variables() { return global_variables_; }
/// Adds an entry point to the module /// Adds an entry point to the module
/// @param ep the entry point to add /// @param ep the entry point to add
void AddEntryPoint(std::unique_ptr<EntryPoint> ep) { void AddEntryPoint(std::unique_ptr<EntryPoint> ep) {