tint: Make ScopeStack key type generic
The scope stack has potential uses other than mapping from symbols. Change-Id: I1eaedb9a9c913a7b2cda41bb99a986c552a66110 Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/89720 Reviewed-by: Ben Clayton <bclayton@google.com>
This commit is contained in:
parent
791b4351d1
commit
2cf32b13d7
|
@ -420,7 +420,7 @@ class DependencyScanner {
|
||||||
DependencyGraph& graph_;
|
DependencyGraph& graph_;
|
||||||
DependencyEdges& dependency_edges_;
|
DependencyEdges& dependency_edges_;
|
||||||
|
|
||||||
ScopeStack<const ast::Node*> scope_stack_;
|
ScopeStack<Symbol, const ast::Node*> scope_stack_;
|
||||||
Global* current_global_ = nullptr;
|
Global* current_global_ = nullptr;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -24,7 +24,7 @@ namespace tint {
|
||||||
|
|
||||||
/// Used to store a stack of scope information.
|
/// Used to store a stack of scope information.
|
||||||
/// The stack starts with a global scope which can not be popped.
|
/// The stack starts with a global scope which can not be popped.
|
||||||
template <class T>
|
template <class K, class V>
|
||||||
class ScopeStack {
|
class ScopeStack {
|
||||||
public:
|
public:
|
||||||
/// Constructor
|
/// Constructor
|
||||||
|
@ -47,32 +47,32 @@ class ScopeStack {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Assigns the value into the top most scope of the stack.
|
/// Assigns the value into the top most scope of the stack.
|
||||||
/// @param symbol the symbol of the value
|
/// @param key the key of the value
|
||||||
/// @param val the value
|
/// @param val the value
|
||||||
/// @returns the old value if there was an existing symbol at the top of the
|
/// @returns the old value if there was an existing key at the top of the
|
||||||
/// stack, otherwise the zero initializer for type T.
|
/// stack, otherwise the zero initializer for type T.
|
||||||
T Set(const Symbol& symbol, T val) {
|
V Set(const K& key, V val) {
|
||||||
std::swap(val, stack_.back()[symbol]);
|
std::swap(val, stack_.back()[key]);
|
||||||
return val;
|
return val;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Retrieves a value from the stack
|
/// Retrieves a value from the stack
|
||||||
/// @param symbol the symbol to look for
|
/// @param key the key to look for
|
||||||
/// @returns the value, or the zero initializer if the value was not found
|
/// @returns the value, or the zero initializer if the value was not found
|
||||||
T Get(const Symbol& symbol) const {
|
V Get(const K& key) const {
|
||||||
for (auto iter = stack_.rbegin(); iter != stack_.rend(); ++iter) {
|
for (auto iter = stack_.rbegin(); iter != stack_.rend(); ++iter) {
|
||||||
auto& map = *iter;
|
auto& map = *iter;
|
||||||
auto val = map.find(symbol);
|
auto val = map.find(key);
|
||||||
if (val != map.end()) {
|
if (val != map.end()) {
|
||||||
return val->second;
|
return val->second;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return T{};
|
return V{};
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::vector<std::unordered_map<Symbol, T>> stack_;
|
std::vector<std::unordered_map<K, V>> stack_;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace tint
|
} // namespace tint
|
||||||
|
|
|
@ -22,7 +22,7 @@ namespace {
|
||||||
class ScopeStackTest : public ProgramBuilder, public testing::Test {};
|
class ScopeStackTest : public ProgramBuilder, public testing::Test {};
|
||||||
|
|
||||||
TEST_F(ScopeStackTest, Get) {
|
TEST_F(ScopeStackTest, Get) {
|
||||||
ScopeStack<uint32_t> s;
|
ScopeStack<Symbol, uint32_t> s;
|
||||||
Symbol a(1, ID());
|
Symbol a(1, ID());
|
||||||
Symbol b(3, ID());
|
Symbol b(3, ID());
|
||||||
s.Push();
|
s.Push();
|
||||||
|
@ -44,13 +44,13 @@ TEST_F(ScopeStackTest, Get) {
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(ScopeStackTest, Get_MissingSymbol) {
|
TEST_F(ScopeStackTest, Get_MissingSymbol) {
|
||||||
ScopeStack<uint32_t> s;
|
ScopeStack<Symbol, uint32_t> s;
|
||||||
Symbol sym(1, ID());
|
Symbol sym(1, ID());
|
||||||
EXPECT_EQ(s.Get(sym), 0u);
|
EXPECT_EQ(s.Get(sym), 0u);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(ScopeStackTest, Set) {
|
TEST_F(ScopeStackTest, Set) {
|
||||||
ScopeStack<uint32_t> s;
|
ScopeStack<Symbol, uint32_t> s;
|
||||||
Symbol a(1, ID());
|
Symbol a(1, ID());
|
||||||
Symbol b(2, ID());
|
Symbol b(2, ID());
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue