2020-12-10 16:56:02 +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.
|
|
|
|
|
|
|
|
#include "src/symbol_table.h"
|
|
|
|
|
2021-04-15 18:20:03 +00:00
|
|
|
#include "src/debug.h"
|
|
|
|
|
2020-12-10 16:56:02 +00:00
|
|
|
namespace tint {
|
|
|
|
|
2021-04-15 18:20:03 +00:00
|
|
|
SymbolTable::SymbolTable(tint::ProgramID program_id)
|
|
|
|
: program_id_(program_id) {}
|
2020-12-10 16:56:02 +00:00
|
|
|
|
2020-12-11 18:24:53 +00:00
|
|
|
SymbolTable::SymbolTable(const SymbolTable&) = default;
|
|
|
|
|
2020-12-10 16:56:02 +00:00
|
|
|
SymbolTable::SymbolTable(SymbolTable&&) = default;
|
|
|
|
|
|
|
|
SymbolTable::~SymbolTable() = default;
|
|
|
|
|
2020-12-11 18:24:53 +00:00
|
|
|
SymbolTable& SymbolTable::operator=(const SymbolTable& other) = default;
|
|
|
|
|
2020-12-10 16:56:02 +00:00
|
|
|
SymbolTable& SymbolTable::operator=(SymbolTable&&) = default;
|
|
|
|
|
|
|
|
Symbol SymbolTable::Register(const std::string& name) {
|
|
|
|
if (name == "")
|
|
|
|
return Symbol();
|
|
|
|
|
|
|
|
auto it = name_to_symbol_.find(name);
|
|
|
|
if (it != name_to_symbol_.end())
|
|
|
|
return it->second;
|
|
|
|
|
2021-04-15 18:20:03 +00:00
|
|
|
Symbol sym(next_symbol_, program_id_);
|
2020-12-10 16:56:02 +00:00
|
|
|
++next_symbol_;
|
|
|
|
|
|
|
|
name_to_symbol_[name] = sym;
|
2021-01-11 22:02:42 +00:00
|
|
|
symbol_to_name_[sym] = name;
|
2020-12-10 16:56:02 +00:00
|
|
|
|
|
|
|
return sym;
|
|
|
|
}
|
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
Symbol SymbolTable::Get(const std::string& name) const {
|
2020-12-11 18:24:53 +00:00
|
|
|
auto it = name_to_symbol_.find(name);
|
|
|
|
return it != name_to_symbol_.end() ? it->second : Symbol();
|
|
|
|
}
|
|
|
|
|
2020-12-10 16:56:02 +00:00
|
|
|
std::string SymbolTable::NameFor(const Symbol symbol) const {
|
2021-04-15 18:20:03 +00:00
|
|
|
TINT_ASSERT_PROGRAM_IDS_EQUAL(program_id_, symbol);
|
2021-01-11 22:02:42 +00:00
|
|
|
auto it = symbol_to_name_.find(symbol);
|
2021-02-11 14:54:41 +00:00
|
|
|
if (it == symbol_to_name_.end()) {
|
|
|
|
return symbol.to_str();
|
|
|
|
}
|
2020-12-10 16:56:02 +00:00
|
|
|
|
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
|
2021-04-13 20:07:57 +00:00
|
|
|
Symbol SymbolTable::New(std::string prefix /* = "tint_symbol" */) {
|
|
|
|
auto it = name_to_symbol_.find(prefix);
|
|
|
|
if (it == name_to_symbol_.end()) {
|
|
|
|
return Register(prefix);
|
|
|
|
}
|
|
|
|
std::string name;
|
|
|
|
size_t i = 1;
|
|
|
|
do {
|
|
|
|
name = prefix + "_" + std::to_string(i++);
|
|
|
|
} while (name_to_symbol_.count(name));
|
|
|
|
return Register(name);
|
2021-02-11 14:54:41 +00:00
|
|
|
}
|
|
|
|
|
2020-12-10 16:56:02 +00:00
|
|
|
} // namespace tint
|