Skip to content

Commit

Permalink
[tblgen] Use std::variant to simplify code. NFCI.
Browse files Browse the repository at this point in the history
  • Loading branch information
d0k committed Aug 27, 2022
1 parent b831af5 commit af14c41
Showing 1 changed file with 11 additions and 27 deletions.
38 changes: 11 additions & 27 deletions llvm/utils/TableGen/DFAEmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include <map>
#include <set>
#include <string>
#include <variant>
#include <vector>

#define DEBUG_TYPE "dfa-emitter"
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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!");
Expand Down Expand Up @@ -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 << ")";
Expand Down

0 comments on commit af14c41

Please sign in to comment.