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_H_ 16 #define SRC_SYMBOL_H_ 17 18 // If TINT_SYMBOL_STORE_DEBUG_NAME is 1, Symbol instances store a `debug_name_` 19 // member initialized with the name of the identifier they represent. This 20 // member is not exposed, but is useful for debugging purposes. 21 #ifndef TINT_SYMBOL_STORE_DEBUG_NAME 22 #define TINT_SYMBOL_STORE_DEBUG_NAME 0 23 #endif 24 25 #include <string> 26 27 #include "src/program_id.h" 28 29 namespace tint { 30 31 /// A symbol representing a string in the system 32 class Symbol { 33 public: 34 /// Constructor 35 /// An invalid symbol 36 Symbol(); 37 /// Constructor 38 /// @param val the symbol value 39 /// @param program_id the identifier of the program that owns this Symbol 40 Symbol(uint32_t val, tint::ProgramID program_id); 41 #if TINT_SYMBOL_STORE_DEBUG_NAME 42 /// Constructor 43 /// @param val the symbol value 44 /// @param program_id the identifier of the program that owns this Symbol 45 /// @param debug_name name of symbols used only for debugging 46 Symbol(uint32_t val, tint::ProgramID program_id, std::string debug_name); 47 #endif 48 /// Copy constructor 49 /// @param o the symbol to copy 50 Symbol(const Symbol& o); 51 /// Move constructor 52 /// @param o the symbol to move 53 Symbol(Symbol&& o); 54 /// Destructor 55 ~Symbol(); 56 57 /// Copy assignment 58 /// @param o the other symbol 59 /// @returns the symbol after doing the copy 60 Symbol& operator=(const Symbol& o); 61 /// Move assignment 62 /// @param o the other symbol 63 /// @returns teh symbol after doing the move 64 Symbol& operator=(Symbol&& o); 65 66 /// Comparison operator 67 /// @param o the other symbol 68 /// @returns true if the symbols are the same 69 bool operator==(const Symbol& o) const; 70 71 /// Less-than operator 72 /// @param o the other symbol 73 /// @returns true if this symbol is ordered before symbol `o` 74 bool operator<(const Symbol& o) const; 75 76 /// @returns true if the symbol is valid IsValid()77 bool IsValid() const { return val_ != static_cast<uint32_t>(-1); } 78 79 /// @returns the value for the symbol value()80 uint32_t value() const { return val_; } 81 82 /// Convert the symbol to a string 83 /// @return the string representation of the symbol 84 std::string to_str() const; 85 86 /// @returns the identifier of the Program that owns this symbol. ProgramID()87 tint::ProgramID ProgramID() const { return program_id_; } 88 89 private: 90 uint32_t val_ = static_cast<uint32_t>(-1); 91 tint::ProgramID program_id_; 92 #if TINT_SYMBOL_STORE_DEBUG_NAME 93 std::string debug_name_; 94 #endif 95 }; 96 97 /// @param sym the Symbol 98 /// @returns the ProgramID that owns the given Symbol ProgramIDOf(Symbol sym)99inline ProgramID ProgramIDOf(Symbol sym) { 100 return sym.IsValid() ? sym.ProgramID() : ProgramID(); 101 } 102 103 } // namespace tint 104 105 namespace std { 106 107 /// Custom std::hash specialization for tint::Symbol so symbols can be used as 108 /// keys for std::unordered_map and std::unordered_set. 109 template <> 110 class hash<tint::Symbol> { 111 public: 112 /// @param sym the symbol to return 113 /// @return the Symbol internal value operator()114 inline std::size_t operator()(const tint::Symbol& sym) const { 115 return static_cast<std::size_t>(sym.value()); 116 } 117 }; 118 119 } // namespace std 120 121 #endif // SRC_SYMBOL_H_ 122