1 // Copyright 2021 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_TRANSFORM_RENAMER_H_ 16 #define SRC_TRANSFORM_RENAMER_H_ 17 18 #include <string> 19 #include <unordered_map> 20 21 #include "src/transform/transform.h" 22 23 namespace tint { 24 namespace transform { 25 26 /// Renamer is a Transform that renames all the symbols in a program. 27 class Renamer : public Castable<Renamer, Transform> { 28 public: 29 /// Data is outputted by the Renamer transform. 30 /// Data holds information about shader usage and constant buffer offsets. 31 struct Data : public Castable<Data, transform::Data> { 32 /// Remappings is a map of old symbol name to new symbol name 33 using Remappings = std::unordered_map<std::string, std::string>; 34 35 /// Constructor 36 /// @param remappings the symbol remappings 37 explicit Data(Remappings&& remappings); 38 39 /// Copy constructor 40 Data(const Data&); 41 42 /// Destructor 43 ~Data() override; 44 45 /// A map of old symbol name to new symbol name 46 const Remappings remappings; 47 }; 48 49 /// Target is an enumerator of rename targets that can be used 50 enum class Target { 51 /// Rename every symbol. 52 kAll, 53 /// Only rename symbols that are reserved keywords in GLSL. 54 kGlslKeywords, 55 /// Only rename symbols that are reserved keywords in HLSL. 56 kHlslKeywords, 57 /// Only rename symbols that are reserved keywords in MSL. 58 kMslKeywords, 59 }; 60 61 /// Optional configuration options for the transform. 62 /// If omitted, then the renamer will use Target::kAll. 63 struct Config : public Castable<Config, transform::Data> { 64 /// Constructor 65 /// @param tgt the targets to rename 66 explicit Config(Target tgt); 67 68 /// Copy constructor 69 Config(const Config&); 70 71 /// Destructor 72 ~Config() override; 73 74 /// The targets to rename 75 Target const target = Target::kAll; 76 }; 77 78 /// Constructor using a the configuration provided in the input Data 79 Renamer(); 80 81 /// Destructor 82 ~Renamer() override; 83 84 /// Runs the transform on `program`, returning the transformation result. 85 /// @param program the source program to transform 86 /// @param data optional extra transform-specific input data 87 /// @returns the transformation result 88 Output Run(const Program* program, const DataMap& data = {}) override; 89 }; 90 91 } // namespace transform 92 } // namespace tint 93 94 #endif // SRC_TRANSFORM_RENAMER_H_ 95