2020-04-03 02:35:23 +00:00
|
|
|
// Copyright 2020 The Tint Authors. //
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2022-02-21 15:19:07 +00:00
|
|
|
#ifndef SRC_TINT_SCOPE_STACK_H_
|
|
|
|
#define SRC_TINT_SCOPE_STACK_H_
|
2020-04-03 02:35:23 +00:00
|
|
|
|
2021-11-22 11:44:57 +00:00
|
|
|
#include <utility>
|
2020-04-03 02:35:23 +00:00
|
|
|
|
2022-02-21 15:19:07 +00:00
|
|
|
#include "src/tint/symbol.h"
|
2022-11-09 20:55:33 +00:00
|
|
|
#include "src/tint/utils/hashmap.h"
|
|
|
|
#include "src/tint/utils/vector.h"
|
2021-01-11 15:10:19 +00:00
|
|
|
|
2020-04-03 02:35:23 +00:00
|
|
|
namespace tint {
|
|
|
|
|
|
|
|
/// Used to store a stack of scope information.
|
|
|
|
/// The stack starts with a global scope which can not be popped.
|
2022-05-11 13:50:33 +00:00
|
|
|
template <class K, class V>
|
2020-04-03 02:35:23 +00:00
|
|
|
class ScopeStack {
|
2022-05-01 14:40:55 +00:00
|
|
|
public:
|
|
|
|
/// Push a new scope on to the stack
|
2022-11-09 20:55:33 +00:00
|
|
|
void Push() { stack_.Push({}); }
|
2020-04-03 02:35:23 +00:00
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
/// Pop the scope off the top of the stack
|
|
|
|
void Pop() {
|
2022-11-09 20:55:33 +00:00
|
|
|
if (stack_.Length() > 1) {
|
|
|
|
stack_.Pop();
|
2022-05-01 14:40:55 +00:00
|
|
|
}
|
2020-04-03 02:35:23 +00:00
|
|
|
}
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
/// Assigns the value into the top most scope of the stack.
|
2022-05-11 13:50:33 +00:00
|
|
|
/// @param key the key of the value
|
2022-05-01 14:40:55 +00:00
|
|
|
/// @param val the value
|
2022-05-11 13:50:33 +00:00
|
|
|
/// @returns the old value if there was an existing key at the top of the
|
2022-05-01 14:40:55 +00:00
|
|
|
/// stack, otherwise the zero initializer for type T.
|
2022-05-11 13:50:33 +00:00
|
|
|
V Set(const K& key, V val) {
|
2022-11-09 20:55:33 +00:00
|
|
|
auto& back = stack_.Back();
|
|
|
|
if (auto* el = back.Find(key)) {
|
|
|
|
std::swap(val, *el);
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
back.Add(key, val);
|
|
|
|
return {};
|
2020-04-03 02:35:23 +00:00
|
|
|
}
|
2021-11-04 22:29:22 +00:00
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
/// Retrieves a value from the stack
|
2022-05-11 13:50:33 +00:00
|
|
|
/// @param key the key to look for
|
2022-05-01 14:40:55 +00:00
|
|
|
/// @returns the value, or the zero initializer if the value was not found
|
2022-05-11 13:50:33 +00:00
|
|
|
V Get(const K& key) const {
|
2022-05-01 14:40:55 +00:00
|
|
|
for (auto iter = stack_.rbegin(); iter != stack_.rend(); ++iter) {
|
2022-11-09 20:55:33 +00:00
|
|
|
if (auto* val = iter->Find(key)) {
|
|
|
|
return *val;
|
2022-05-01 14:40:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-11 13:50:33 +00:00
|
|
|
return V{};
|
2022-05-01 14:40:55 +00:00
|
|
|
}
|
2020-04-03 02:35:23 +00:00
|
|
|
|
2022-05-11 22:05:15 +00:00
|
|
|
/// Return the top scope of the stack.
|
|
|
|
/// @returns the top scope of the stack
|
2022-11-09 20:55:33 +00:00
|
|
|
const utils::Hashmap<K, V, 8>& Top() const { return stack_.Back(); }
|
2022-05-11 22:05:15 +00:00
|
|
|
|
|
|
|
/// Clear the scope stack.
|
|
|
|
void Clear() {
|
2022-11-09 20:55:33 +00:00
|
|
|
stack_.Clear();
|
|
|
|
stack_.Push({});
|
2022-05-11 22:05:15 +00:00
|
|
|
}
|
|
|
|
|
2022-05-01 14:40:55 +00:00
|
|
|
private:
|
2022-11-09 20:55:33 +00:00
|
|
|
utils::Vector<utils::Hashmap<K, V, 8>, 8> stack_ = {{}};
|
2020-04-03 02:35:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace tint
|
|
|
|
|
2022-02-21 15:19:07 +00:00
|
|
|
#endif // SRC_TINT_SCOPE_STACK_H_
|