Remove BlockStatement::insert()

Bug: tint:396
Bug: tint:390
Change-Id: I719b84804164fa801ded505ed56717948f06c7a7
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/35502
Commit-Queue: Ben Clayton <bclayton@google.com>
Reviewed-by: dan sinclair <dsinclair@chromium.org>
This commit is contained in:
Ben Clayton
2020-12-14 20:31:17 +00:00
committed by Commit Bot service account
parent db5ce658b5
commit d408f2465a
16 changed files with 349 additions and 299 deletions

View File

@@ -35,14 +35,6 @@ class BlockStatement : public Castable<BlockStatement, Statement> {
BlockStatement(BlockStatement&&);
~BlockStatement() override;
/// Insert a statement to the block
/// @param index the index to insert at
/// @param stmt the statement to insert
void insert(size_t index, Statement* stmt) {
auto offset = static_cast<decltype(statements_)::difference_type>(index);
statements_.insert(statements_.begin() + offset, stmt);
}
/// @returns true if the block is empty
bool empty() const { return statements_.empty(); }
/// @returns the number of statements directly in the block
@@ -60,16 +52,12 @@ class BlockStatement : public Castable<BlockStatement, Statement> {
/// Retrieves the statement at `idx`
/// @param idx the index. The index is not bounds checked.
/// @returns the statement at `idx`
const Statement* get(size_t idx) const { return statements_[idx]; }
Statement* get(size_t idx) const { return statements_[idx]; }
/// Retrieves the statement at `idx`
/// @param idx the index. The index is not bounds checked.
/// @returns the statement at `idx`
Statement* operator[](size_t idx) { return statements_[idx]; }
/// Retrieves the statement at `idx`
/// @param idx the index. The index is not bounds checked.
/// @returns the statement at `idx`
const Statement* operator[](size_t idx) const { return statements_[idx]; }
Statement* operator[](size_t idx) const { return statements_[idx]; }
/// @returns the beginning iterator
StatementList::const_iterator begin() const { return statements_.begin(); }

View File

@@ -37,24 +37,6 @@ TEST_F(BlockStatementTest, Creation) {
EXPECT_EQ(b[0], ptr);
}
TEST_F(BlockStatementTest, Creation_WithInsert) {
auto* s1 = create<DiscardStatement>(Source{});
auto* s2 = create<DiscardStatement>(Source{});
auto* s3 = create<DiscardStatement>(Source{});
BlockStatement b(Source{}, StatementList{});
b.insert(0, s1);
b.insert(0, s2);
b.insert(1, s3);
// |b| should contain s2, s3, s1
ASSERT_EQ(b.size(), 3u);
EXPECT_EQ(b[0], s2);
EXPECT_EQ(b[1], s3);
EXPECT_EQ(b[2], s1);
}
TEST_F(BlockStatementTest, Creation_WithSource) {
BlockStatement b(Source{Source::Location{20, 2}}, ast::StatementList{});
auto src = b.source();

View File

@@ -33,15 +33,30 @@ Module::~Module() = default;
Module Module::Clone() {
Module out;
CloneContext ctx(&out);
Clone(&ctx);
// Symbol table must be cloned first so that the resulting module has the
// symbols before we start the tree mutations.
ctx.mod->symbol_table_ = symbol_table_;
CloneUsing(&ctx);
return out;
}
void Module::Clone(CloneContext* ctx) {
Module Module::Clone(const std::function<void(CloneContext* ctx)>& init) {
Module out;
CloneContext ctx(&out);
// Symbol table must be cloned first so that the resulting module has the
// symbols before we start the tree mutations.
ctx->mod->symbol_table_ = symbol_table_;
ctx.mod->symbol_table_ = symbol_table_;
init(&ctx);
CloneUsing(&ctx);
return out;
}
void Module::CloneUsing(CloneContext* ctx) {
for (auto* ty : constructed_types_) {
ctx->mod->constructed_types_.emplace_back(ctx->Clone(ty));
}

View File

@@ -15,6 +15,7 @@
#ifndef SRC_AST_MODULE_H_
#define SRC_AST_MODULE_H_
#include <functional>
#include <memory>
#include <string>
#include <type_traits>
@@ -55,13 +56,11 @@ class Module {
/// @return a deep copy of this module
Module Clone();
/// Clone this module into `ctx->mod` using the provided CloneContext
/// The module will be cloned in this order:
/// * Constructed types
/// * Global variables
/// * Functions
/// @param ctx the clone context
void Clone(CloneContext* ctx);
/// @param init a callback function to configure the CloneContex before
/// cloning any of the module's state
/// @return a deep copy of this module, calling `init` to first initialize the
/// context.
Module Clone(const std::function<void(CloneContext* ctx)>& init);
/// Add a global variable to the module
/// @param var the variable to add
@@ -181,6 +180,14 @@ class Module {
private:
Module(const Module&) = delete;
/// Clone this module into `ctx->mod` using the provided CloneContext
/// The module will be cloned in this order:
/// * Constructed types
/// * Global variables
/// * Functions
/// @param ctx the clone context
void CloneUsing(CloneContext* ctx);
SymbolTable symbol_table_;
VariableList global_variables_;
// The constructed types are owned by the type manager