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 #include #include +#include #include #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; -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; using ActionTuple = std::vector; 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(SymbolV->getType())) { - Actions.emplace_back(R->getValueAsDef(A), 0, ""); + Actions.emplace_back(R->getValueAsDef(A)); Types.emplace_back(Ty->getAsString()); } else if (isa(SymbolV->getType())) { - Actions.emplace_back(nullptr, R->getValueAsInt(A), ""); + Actions.emplace_back(static_cast(R->getValueAsInt(A))); Types.emplace_back("unsigned"); } else if (isa(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(&SingleAction)) + OS << (*R)->getName(); + else if (const auto *S = std::get_if(&SingleAction)) + OS << '"' << *S << '"'; + else + OS << std::get(SingleAction); } if (AT.size() > 1) OS << ")";