blob: 2b20fed9a088556f7fc0948b8f2ec4837fe22c24 [file] [log] [blame]
Ryan Harrisondbc13af2022-02-21 15:19:07 +00001// Copyright 2020 The Tint Authors.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#include "src/tint/symbol_table.h"
16
17#include "src/tint/debug.h"
18
19namespace tint {
20
dan sinclair41e4d9a2022-05-01 14:40:55 +000021SymbolTable::SymbolTable(tint::ProgramID program_id) : program_id_(program_id) {}
Ryan Harrisondbc13af2022-02-21 15:19:07 +000022
23SymbolTable::SymbolTable(const SymbolTable&) = default;
24
25SymbolTable::SymbolTable(SymbolTable&&) = default;
26
27SymbolTable::~SymbolTable() = default;
28
29SymbolTable& SymbolTable::operator=(const SymbolTable& other) = default;
30
31SymbolTable& SymbolTable::operator=(SymbolTable&&) = default;
32
33Symbol SymbolTable::Register(const std::string& name) {
dan sinclair41e4d9a2022-05-01 14:40:55 +000034 TINT_ASSERT(Symbol, !name.empty());
Ryan Harrisondbc13af2022-02-21 15:19:07 +000035
dan sinclair41e4d9a2022-05-01 14:40:55 +000036 auto it = name_to_symbol_.find(name);
Austin Eng86a617f2022-05-19 20:08:19 +000037 if (it != name_to_symbol_.end()) {
dan sinclair41e4d9a2022-05-01 14:40:55 +000038 return it->second;
Austin Eng86a617f2022-05-19 20:08:19 +000039 }
Ryan Harrisondbc13af2022-02-21 15:19:07 +000040
41#if TINT_SYMBOL_STORE_DEBUG_NAME
dan sinclair41e4d9a2022-05-01 14:40:55 +000042 Symbol sym(next_symbol_, program_id_, name);
Ryan Harrisondbc13af2022-02-21 15:19:07 +000043#else
dan sinclair41e4d9a2022-05-01 14:40:55 +000044 Symbol sym(next_symbol_, program_id_);
Ryan Harrisondbc13af2022-02-21 15:19:07 +000045#endif
dan sinclair41e4d9a2022-05-01 14:40:55 +000046 ++next_symbol_;
Ryan Harrisondbc13af2022-02-21 15:19:07 +000047
dan sinclair41e4d9a2022-05-01 14:40:55 +000048 name_to_symbol_[name] = sym;
49 symbol_to_name_[sym] = name;
Ryan Harrisondbc13af2022-02-21 15:19:07 +000050
dan sinclair41e4d9a2022-05-01 14:40:55 +000051 return sym;
Ryan Harrisondbc13af2022-02-21 15:19:07 +000052}
53
54Symbol SymbolTable::Get(const std::string& name) const {
dan sinclair41e4d9a2022-05-01 14:40:55 +000055 auto it = name_to_symbol_.find(name);
56 return it != name_to_symbol_.end() ? it->second : Symbol();
Ryan Harrisondbc13af2022-02-21 15:19:07 +000057}
58
59std::string SymbolTable::NameFor(const Symbol symbol) const {
dan sinclair41e4d9a2022-05-01 14:40:55 +000060 TINT_ASSERT_PROGRAM_IDS_EQUAL(Symbol, program_id_, symbol);
61 auto it = symbol_to_name_.find(symbol);
62 if (it == symbol_to_name_.end()) {
63 return symbol.to_str();
64 }
Ryan Harrisondbc13af2022-02-21 15:19:07 +000065
dan sinclair41e4d9a2022-05-01 14:40:55 +000066 return it->second;
Ryan Harrisondbc13af2022-02-21 15:19:07 +000067}
68
69Symbol SymbolTable::New(std::string prefix /* = "" */) {
dan sinclair41e4d9a2022-05-01 14:40:55 +000070 if (prefix.empty()) {
71 prefix = "tint_symbol";
72 }
73 auto it = name_to_symbol_.find(prefix);
74 if (it == name_to_symbol_.end()) {
75 return Register(prefix);
76 }
77 std::string name;
78 size_t i = 1;
79 do {
80 name = prefix + "_" + std::to_string(i++);
81 } while (name_to_symbol_.count(name));
82 return Register(name);
Ryan Harrisondbc13af2022-02-21 15:19:07 +000083}
84
85} // namespace tint