[llvm] af14c41 - [tblgen] Use std::variant to simplify code. NFCI.

Benjamin Kramer via llvm-commits llvm-commits at lists.llvm.org
Sat Aug 27 01:54:54 PDT 2022


Author: Benjamin Kramer
Date: 2022-08-27T10:50:28+02:00
New Revision: af14c41d070f10f94a94016723f772b3920d8719

URL: https://github.com/llvm/llvm-project/commit/af14c41d070f10f94a94016723f772b3920d8719
DIFF: https://github.com/llvm/llvm-project/commit/af14c41d070f10f94a94016723f772b3920d8719.diff

LOG: [tblgen] Use std::variant to simplify code. NFCI.

Added: 
    

Modified: 
    llvm/utils/TableGen/DFAEmitter.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/utils/TableGen/DFAEmitter.cpp b/llvm/utils/TableGen/DFAEmitter.cpp
index f2d9165c5c8c8..705908226fa18 100644
--- a/llvm/utils/TableGen/DFAEmitter.cpp
+++ b/llvm/utils/TableGen/DFAEmitter.cpp
@@ -35,6 +35,7 @@
 #include <map>
 #include <set>
 #include <string>
+#include <variant>
 #include <vector>
 
 #define DEBUG_TYPE "dfa-emitter"
@@ -169,30 +170,8 @@ void DfaEmitter::printActionValue(action_type A, raw_ostream &OS) { OS << A; }
 //===----------------------------------------------------------------------===//
 
 namespace {
-// FIXME: This entire discriminated union could be removed with c++17:
-//   using Action = std::variant<Record *, unsigned, std::string>;
-struct Action {
-  Record *R = nullptr;
-  unsigned I = 0;
-  std::string S;
-
-  Action() = default;
-  Action(Record *R, unsigned I, std::string S) : R(R), I(I), S(S) {}
-
-  void print(raw_ostream &OS) const {
-    if (R)
-      OS << R->getName();
-    else if (!S.empty())
-      OS << '"' << S << '"';
-    else
-      OS << I;
-  }
-  bool operator<(const Action &Other) const {
-    return std::make_tuple(R, I, S) <
-           std::make_tuple(Other.R, Other.I, Other.S);
-  }
-};
 
+using Action = std::variant<Record *, unsigned, std::string>;
 using ActionTuple = std::vector<Action>;
 class Automaton;
 
@@ -342,13 +321,13 @@ Transition::Transition(Record *R, Automaton *Parent) {
   for (StringRef A : Parent->getActionSymbolFields()) {
     RecordVal *SymbolV = R->getValue(A);
     if (auto *Ty = dyn_cast<RecordRecTy>(SymbolV->getType())) {
-      Actions.emplace_back(R->getValueAsDef(A), 0, "");
+      Actions.emplace_back(R->getValueAsDef(A));
       Types.emplace_back(Ty->getAsString());
     } else if (isa<IntRecTy>(SymbolV->getType())) {
-      Actions.emplace_back(nullptr, R->getValueAsInt(A), "");
+      Actions.emplace_back(static_cast<unsigned>(R->getValueAsInt(A)));
       Types.emplace_back("unsigned");
     } else if (isa<StringRecTy>(SymbolV->getType())) {
-      Actions.emplace_back(nullptr, 0, std::string(R->getValueAsString(A)));
+      Actions.emplace_back(std::string(R->getValueAsString(A)));
       Types.emplace_back("std::string");
     } else {
       report_fatal_error("Unhandled symbol type!");
@@ -380,7 +359,12 @@ void CustomDfaEmitter::printActionValue(action_type A, raw_ostream &OS) {
   ListSeparator LS;
   for (const auto &SingleAction : AT) {
     OS << LS;
-    SingleAction.print(OS);
+    if (const auto *R = std::get_if<Record *>(&SingleAction))
+      OS << (*R)->getName();
+    else if (const auto *S = std::get_if<std::string>(&SingleAction))
+      OS << '"' << *S << '"';
+    else
+      OS << std::get<unsigned>(SingleAction);
   }
   if (AT.size() > 1)
     OS << ")";


        


More information about the llvm-commits mailing list