• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef SRC_SYMBOL_TABLE_H_
16 #define SRC_SYMBOL_TABLE_H_
17 
18 #include <string>
19 #include <unordered_map>
20 
21 #include "src/symbol.h"
22 
23 namespace tint {
24 
25 /// Holds mappings from symbols to their associated string names
26 class SymbolTable {
27  public:
28   /// Constructor
29   /// @param program_id the identifier of the program that owns this symbol
30   /// table
31   explicit SymbolTable(tint::ProgramID program_id);
32   /// Copy constructor
33   SymbolTable(const SymbolTable&);
34   /// Move Constructor
35   SymbolTable(SymbolTable&&);
36   /// Destructor
37   ~SymbolTable();
38 
39   /// Copy assignment
40   /// @param other the symbol table to copy
41   /// @returns the new symbol table
42   SymbolTable& operator=(const SymbolTable& other);
43   /// Move assignment
44   /// @param other the symbol table to move
45   /// @returns the symbol table
46   SymbolTable& operator=(SymbolTable&& other);
47 
48   /// Registers a name into the symbol table, returning the Symbol.
49   /// @param name the name to register
50   /// @returns the symbol representing the given name
51   Symbol Register(const std::string& name);
52 
53   /// Returns the symbol for the given `name`
54   /// @param name the name to lookup
55   /// @returns the symbol for the name or symbol::kInvalid if not found.
56   Symbol Get(const std::string& name) const;
57 
58   /// Returns the name for the given symbol
59   /// @param symbol the symbol to retrieve the name for
60   /// @returns the symbol name or "" if not found
61   std::string NameFor(const Symbol symbol) const;
62 
63   /// Returns a new unique symbol with the given name, possibly suffixed with a
64   /// unique number.
65   /// @param name the symbol name
66   /// @returns a new, unnamed symbol with the given name. If the name is already
67   /// taken then this will be suffixed with an underscore and a unique numerical
68   /// value
69   Symbol New(std::string name = "");
70 
71   /// Foreach calls the callback function `F` for each symbol in the table.
72   /// @param callback must be a function or function-like object with the
73   /// signature: `void(Symbol, const std::string&)`
74   template <typename F>
Foreach(F && callback)75   void Foreach(F&& callback) const {
76     for (auto it : symbol_to_name_) {
77       callback(it.first, it.second);
78     }
79   }
80 
81   /// @returns the identifier of the Program that owns this symbol table.
ProgramID()82   tint::ProgramID ProgramID() const { return program_id_; }
83 
84  private:
85   // The value to be associated to the next registered symbol table entry.
86   uint32_t next_symbol_ = 1;
87 
88   std::unordered_map<Symbol, std::string> symbol_to_name_;
89   std::unordered_map<std::string, Symbol> name_to_symbol_;
90   tint::ProgramID program_id_;
91 };
92 
93 /// @param symbol_table the SymbolTable
94 /// @returns the ProgramID that owns the given SymbolTable
ProgramIDOf(const SymbolTable & symbol_table)95 inline ProgramID ProgramIDOf(const SymbolTable& symbol_table) {
96   return symbol_table.ProgramID();
97 }
98 
99 }  // namespace tint
100 
101 #endif  // SRC_SYMBOL_TABLE_H_
102