Namer: Take a SymbolTable instead of a Program

Program is going to undergo some heavy refactoring.
Reduce unnecessary dependencies to what Namer actually wants.

Change-Id: Ie411da113a2728321c52ba0a72ae7c2139469886
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/38543
Reviewed-by: dan sinclair <dsinclair@chromium.org>
This commit is contained in:
Ben Clayton 2021-01-26 16:57:10 +00:00
parent d542a2875f
commit 9f7a414505
3 changed files with 26 additions and 26 deletions

View File

@ -22,7 +22,7 @@
namespace tint { namespace tint {
Namer::Namer(Program* program) : program_(program) {} Namer::Namer(SymbolTable* symbols) : symbols_(symbols) {}
Namer::~Namer() = default; Namer::~Namer() = default;
@ -42,7 +42,7 @@ std::string Namer::GenerateName(const std::string& prefix) {
return name; return name;
} }
MangleNamer::MangleNamer(Program* program) : Namer(program) {} MangleNamer::MangleNamer(SymbolTable* symbols) : Namer(symbols) {}
MangleNamer::~MangleNamer() = default; MangleNamer::~MangleNamer() = default;
@ -50,12 +50,12 @@ std::string MangleNamer::NameFor(const Symbol& sym) {
return sym.to_str(); return sym.to_str();
} }
UnsafeNamer::UnsafeNamer(Program* program) : Namer(program) {} UnsafeNamer::UnsafeNamer(SymbolTable* symbols) : Namer(symbols) {}
UnsafeNamer::~UnsafeNamer() = default; UnsafeNamer::~UnsafeNamer() = default;
std::string UnsafeNamer::NameFor(const Symbol& sym) { std::string UnsafeNamer::NameFor(const Symbol& sym) {
return program_->SymbolToName(sym); return symbols_->NameFor(sym);
} }
} // namespace tint } // namespace tint

View File

@ -19,7 +19,7 @@
#include <unordered_map> #include <unordered_map>
#include <unordered_set> #include <unordered_set>
#include "src/program.h" #include "src/symbol_table.h"
namespace tint { namespace tint {
@ -27,8 +27,8 @@ namespace tint {
class Namer { class Namer {
public: public:
/// Constructor /// Constructor
/// @param program the program this namer works with /// @param symbols the symbol table this namer works with
explicit Namer(Program* program); explicit Namer(SymbolTable* symbols);
/// Destructor /// Destructor
virtual ~Namer(); virtual ~Namer();
@ -48,8 +48,8 @@ class Namer {
/// @returns true if `name` has already been used /// @returns true if `name` has already been used
bool IsUsed(const std::string& name); bool IsUsed(const std::string& name);
/// The program storing the symbol table /// The symbol table
Program* program_ = nullptr; SymbolTable* symbols_ = nullptr;
private: private:
// The list of names taken by the remapper // The list of names taken by the remapper
@ -60,8 +60,8 @@ class Namer {
class MangleNamer : public Namer { class MangleNamer : public Namer {
public: public:
/// Constructor /// Constructor
/// @param program the program to retrieve names from /// @param symbols the symbol table this namer works with
explicit MangleNamer(Program* program); explicit MangleNamer(SymbolTable* symbols);
/// Destructor /// Destructor
~MangleNamer() override; ~MangleNamer() override;
@ -77,8 +77,8 @@ class MangleNamer : public Namer {
class UnsafeNamer : public Namer { class UnsafeNamer : public Namer {
public: public:
/// Constructor /// Constructor
/// @param program the program to retrieve names from /// @param symbols the symbol table this namer works with
explicit UnsafeNamer(Program* program); explicit UnsafeNamer(SymbolTable* symbols);
/// Destructor /// Destructor
~UnsafeNamer() override; ~UnsafeNamer() override;

View File

@ -15,7 +15,7 @@
#include "src/namer.h" #include "src/namer.h"
#include "gtest/gtest.h" #include "gtest/gtest.h"
#include "src/program.h" #include "src/symbol_table.h"
namespace tint { namespace tint {
namespace { namespace {
@ -23,8 +23,8 @@ namespace {
using NamerTest = testing::Test; using NamerTest = testing::Test;
TEST_F(NamerTest, GenerateName) { TEST_F(NamerTest, GenerateName) {
Program m; SymbolTable t;
MangleNamer n(&m); MangleNamer n(&t);
EXPECT_EQ("name", n.GenerateName("name")); EXPECT_EQ("name", n.GenerateName("name"));
EXPECT_EQ("name_0", n.GenerateName("name")); EXPECT_EQ("name_0", n.GenerateName("name"));
EXPECT_EQ("name_1", n.GenerateName("name")); EXPECT_EQ("name_1", n.GenerateName("name"));
@ -33,19 +33,19 @@ TEST_F(NamerTest, GenerateName) {
using MangleNamerTest = testing::Test; using MangleNamerTest = testing::Test;
TEST_F(MangleNamerTest, ReturnsName) { TEST_F(MangleNamerTest, ReturnsName) {
Program m; SymbolTable t;
auto s = m.RegisterSymbol("my_sym"); auto s = t.Register("my_sym");
MangleNamer n(&m); MangleNamer n(&t);
EXPECT_EQ("tint_symbol_1", n.NameFor(s)); EXPECT_EQ("tint_symbol_1", n.NameFor(s));
} }
TEST_F(MangleNamerTest, ReturnsSameValueForSameName) { TEST_F(MangleNamerTest, ReturnsSameValueForSameName) {
Program m; SymbolTable t;
auto s1 = m.RegisterSymbol("my_sym"); auto s1 = t.Register("my_sym");
auto s2 = m.RegisterSymbol("my_sym2"); auto s2 = t.Register("my_sym2");
MangleNamer n(&m); MangleNamer n(&t);
EXPECT_EQ("tint_symbol_1", n.NameFor(s1)); EXPECT_EQ("tint_symbol_1", n.NameFor(s1));
EXPECT_EQ("tint_symbol_2", n.NameFor(s2)); EXPECT_EQ("tint_symbol_2", n.NameFor(s2));
EXPECT_EQ("tint_symbol_1", n.NameFor(s1)); EXPECT_EQ("tint_symbol_1", n.NameFor(s1));
@ -53,10 +53,10 @@ TEST_F(MangleNamerTest, ReturnsSameValueForSameName) {
using UnsafeNamerTest = testing::Test; using UnsafeNamerTest = testing::Test;
TEST_F(UnsafeNamerTest, ReturnsName) { TEST_F(UnsafeNamerTest, ReturnsName) {
Program m; SymbolTable t;
auto s = m.RegisterSymbol("my_sym"); auto s = t.Register("my_sym");
UnsafeNamer n(&m); UnsafeNamer n(&t);
EXPECT_EQ("my_sym", n.NameFor(s)); EXPECT_EQ("my_sym", n.NameFor(s));
} }