Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1 | // 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 | |
| 19 | namespace tint { |
| 20 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 21 | SymbolTable::SymbolTable(tint::ProgramID program_id) : program_id_(program_id) {} |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 22 | |
| 23 | SymbolTable::SymbolTable(const SymbolTable&) = default; |
| 24 | |
| 25 | SymbolTable::SymbolTable(SymbolTable&&) = default; |
| 26 | |
| 27 | SymbolTable::~SymbolTable() = default; |
| 28 | |
| 29 | SymbolTable& SymbolTable::operator=(const SymbolTable& other) = default; |
| 30 | |
| 31 | SymbolTable& SymbolTable::operator=(SymbolTable&&) = default; |
| 32 | |
| 33 | Symbol SymbolTable::Register(const std::string& name) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 34 | TINT_ASSERT(Symbol, !name.empty()); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 35 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 36 | auto it = name_to_symbol_.find(name); |
Austin Eng | 86a617f | 2022-05-19 20:08:19 +0000 | [diff] [blame] | 37 | if (it != name_to_symbol_.end()) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 38 | return it->second; |
Austin Eng | 86a617f | 2022-05-19 20:08:19 +0000 | [diff] [blame] | 39 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 40 | |
| 41 | #if TINT_SYMBOL_STORE_DEBUG_NAME |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 42 | Symbol sym(next_symbol_, program_id_, name); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 43 | #else |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 44 | Symbol sym(next_symbol_, program_id_); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 45 | #endif |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 46 | ++next_symbol_; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 47 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 48 | name_to_symbol_[name] = sym; |
| 49 | symbol_to_name_[sym] = name; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 50 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 51 | return sym; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | Symbol SymbolTable::Get(const std::string& name) const { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 55 | auto it = name_to_symbol_.find(name); |
| 56 | return it != name_to_symbol_.end() ? it->second : Symbol(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | std::string SymbolTable::NameFor(const Symbol symbol) const { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 60 | 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 Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 65 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 66 | return it->second; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | Symbol SymbolTable::New(std::string prefix /* = "" */) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 70 | 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 Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | } // namespace tint |