66//
77// ===----------------------------------------------------------------------===//
88
9- #ifndef IRDLTOCPP_TEMPLATE_UTILS_H
10- #define IRDLTOCPP_TEMPLATE_UTILS_H
9+ #ifndef MLIR_LIB_TARGET_IRDLTOCPP_TEMPLATINGUTILS_H
10+ #define MLIR_LIB_TARGET_IRDLTOCPP_TEMPLATINGUTILS_H
1111
1212#include " llvm/ADT/SmallString.h"
1313#include " llvm/ADT/StringMap.h"
1818
1919namespace mlir ::irdl::detail {
2020
21- // / A dictionary stores a mapping of template variables to their assigned
22- // / variables
21+ // / A dictionary stores a mapping of template variable names to their assigned
22+ // / string values.
2323using dictionary = llvm::StringMap<llvm::SmallString<8 >>;
2424
2525// / Template Code as used by IRDL-to-Cpp.
2626// /
27- // / For efficiency, produces a bytecode representation of an input string
27+ // / For efficiency, produces a bytecode representation of an input template.
2828// / - LiteralToken: A contiguous stream of characters to be printed
2929// / - ReplacementToken: A template variable that will be replaced
3030class Template {
@@ -47,30 +47,28 @@ class Template {
4747 }
4848 }
4949
50- // Render will apply a dictionary to the Template
51- // and send it to the specified output stream
50+ // / Render will apply a dictionary to the Template and send the rendered
51+ // / result to the specified output stream.
5252 void render (llvm::raw_ostream &out, const dictionary &replacements) const {
5353 for (auto instruction : bytecode) {
54- std::visit (
55- [&]( auto && inst) {
56- using T = std:: decay_t < decltype (inst)> ;
57- if constexpr (std::is_same_v<T, LiteralToken>) {
58- out << inst. text ;
59- } else if constexpr ( std::is_same_v<T, ReplacementToken>) {
60- auto replacement = replacements.find (inst. keyName );
54+ if ( auto *inst = std::get_if<LiteralToken>(&instruction)) {
55+ out << inst-> text ;
56+ continue ;
57+ }
58+
59+ if ( auto *inst = std::get_if< ReplacementToken>(&instruction) ) {
60+ auto replacement = replacements.find (inst-> keyName );
6161#ifndef NDEBUG
62- if (replacement == replacements.end ()) {
63- llvm::errs ()
64- << " Missing template key: " << inst.keyName << " \n " ;
65- llvm_unreachable (" Missing template key" );
66- }
62+ if (replacement == replacements.end ()) {
63+ llvm::errs () << " Missing template key: " << inst->keyName << " \n " ;
64+ llvm_unreachable (" Missing template key" );
65+ }
6766#endif
68- out << replacement->second ;
69- } else {
70- static_assert (false , " non-exhaustive visitor!" );
71- }
72- },
73- instruction);
67+ out << replacement->second ;
68+ continue ;
69+ }
70+
71+ llvm_unreachable (" non-exhaustive bytecode visit" );
7472 }
7573 }
7674
@@ -88,4 +86,4 @@ class Template {
8886
8987} // namespace mlir::irdl::detail
9088
91- #endif // #ifndef IRDLTOCPP_TEMPLATE_UTILS_H
89+ #endif // MLIR_LIB_TARGET_IRDLTOCPP_TEMPLATINGUTILS_H
0 commit comments