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.
|
|
|
|
|
|
|
|
#ifndef SRC_SCOPE_STACK_H_
|
|
|
|
#define SRC_SCOPE_STACK_H_
|
|
|
|
|
|
|
|
#include <unordered_map>
|
|
|
|
#include <vector>
|
|
|
|
|
2021-01-11 15:10:19 +00:00
|
|
|
#include "src/symbol.h"
|
|
|
|
|
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.
|
|
|
|
template <class T>
|
|
|
|
class ScopeStack {
|
|
|
|
public:
|
|
|
|
/// Constructor
|
|
|
|
ScopeStack() {
|
|
|
|
// Push global bucket
|
|
|
|
stack_.push_back({});
|
|
|
|
}
|
|
|
|
/// Copy Constructor
|
|
|
|
ScopeStack(const ScopeStack&) = default;
|
|
|
|
~ScopeStack() = default;
|
|
|
|
|
|
|
|
/// Push a new scope on to the stack
|
2021-11-04 22:29:22 +00:00
|
|
|
void Push() { stack_.push_back({}); }
|
2020-04-03 02:35:23 +00:00
|
|
|
|
|
|
|
/// Pop the scope off the top of the stack
|
2021-11-04 22:29:22 +00:00
|
|
|
void Pop() {
|
2020-04-03 02:35:23 +00:00
|
|
|
if (stack_.size() > 1) {
|
|
|
|
stack_.pop_back();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-04 22:29:22 +00:00
|
|
|
/// Assigns the value into the top most scope of the stack
|
2021-01-11 15:10:19 +00:00
|
|
|
/// @param symbol the symbol of the variable
|
2020-04-03 02:35:23 +00:00
|
|
|
/// @param val the value
|
2021-11-04 22:29:22 +00:00
|
|
|
void Set(const Symbol& symbol, T val) { stack_.back()[symbol] = val; }
|
2020-04-03 02:35:23 +00:00
|
|
|
|
2021-11-04 22:29:22 +00:00
|
|
|
/// Retrieves a value from the stack
|
2021-01-11 15:10:19 +00:00
|
|
|
/// @param symbol the symbol to look for
|
2021-11-04 22:29:22 +00:00
|
|
|
/// @returns the variable, or the zero initializer if the value was not found
|
|
|
|
T Get(const Symbol& symbol) const {
|
2020-04-03 02:35:23 +00:00
|
|
|
for (auto iter = stack_.rbegin(); iter != stack_.rend(); ++iter) {
|
|
|
|
auto& map = *iter;
|
2021-01-11 22:02:42 +00:00
|
|
|
auto val = map.find(symbol);
|
2020-04-03 02:35:23 +00:00
|
|
|
if (val != map.end()) {
|
2021-11-04 22:29:22 +00:00
|
|
|
return val->second;
|
2020-04-03 02:35:23 +00:00
|
|
|
}
|
|
|
|
}
|
2021-11-04 22:29:22 +00:00
|
|
|
|
|
|
|
return T{};
|
2020-04-03 02:35:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2021-01-11 22:02:42 +00:00
|
|
|
std::vector<std::unordered_map<Symbol, T>> stack_;
|
2020-04-03 02:35:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace tint
|
|
|
|
|
|
|
|
#endif // SRC_SCOPE_STACK_H_
|