aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/TableGen/JSONBackend.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'llvm/lib/TableGen/JSONBackend.cpp')
-rw-r--r--llvm/lib/TableGen/JSONBackend.cpp185
1 files changed, 90 insertions, 95 deletions
diff --git a/llvm/lib/TableGen/JSONBackend.cpp b/llvm/lib/TableGen/JSONBackend.cpp
index cd10c22..d648019 100644
--- a/llvm/lib/TableGen/JSONBackend.cpp
+++ b/llvm/lib/TableGen/JSONBackend.cpp
@@ -26,43 +26,41 @@ namespace {
class JSONEmitter {
private:
- RecordKeeper &Records;
+ const RecordKeeper &Records;
json::Value translateInit(const Init &I);
public:
- JSONEmitter(RecordKeeper &R);
+ explicit JSONEmitter(const RecordKeeper &R) : Records(R) {}
void run(raw_ostream &OS);
};
} // end anonymous namespace
-JSONEmitter::JSONEmitter(RecordKeeper &R) : Records(R) {}
-
json::Value JSONEmitter::translateInit(const Init &I) {
-
// Init subclasses that we return as JSON primitive values of one
// kind or another.
- if (isa<UnsetInit>(&I)) {
+ if (isa<UnsetInit>(&I))
return nullptr;
- } else if (auto *Bit = dyn_cast<BitInit>(&I)) {
+ if (const auto *Bit = dyn_cast<BitInit>(&I))
return Bit->getValue() ? 1 : 0;
- } else if (auto *Bits = dyn_cast<BitsInit>(&I)) {
- json::Array array;
- for (unsigned i = 0, limit = Bits->getNumBits(); i < limit; i++)
- array.push_back(translateInit(*Bits->getBit(i)));
- return std::move(array);
- } else if (auto *Int = dyn_cast<IntInit>(&I)) {
+ if (const auto *Bits = dyn_cast<BitsInit>(&I)) {
+ json::Array Array;
+ for (unsigned Idx = 0, E = Bits->getNumBits(); Idx < E; ++Idx)
+ Array.push_back(translateInit(*Bits->getBit(Idx)));
+ return std::move(Array);
+ }
+ if (const auto *Int = dyn_cast<IntInit>(&I))
return Int->getValue();
- } else if (auto *Str = dyn_cast<StringInit>(&I)) {
+ if (const auto *Str = dyn_cast<StringInit>(&I))
return Str->getValue();
- } else if (auto *List = dyn_cast<ListInit>(&I)) {
- json::Array array;
- for (auto *val : *List)
- array.push_back(translateInit(*val));
- return std::move(array);
+ if (const auto *List = dyn_cast<ListInit>(&I)) {
+ json::Array Array;
+ for (const auto *Val : *List)
+ Array.push_back(translateInit(*Val));
+ return std::move(Array);
}
// Init subclasses that we return as JSON objects containing a
@@ -70,56 +68,58 @@ json::Value JSONEmitter::translateInit(const Init &I) {
// translation back into TableGen input syntax that -print-records
// would give.
- json::Object obj;
- obj["printable"] = I.getAsString();
-
- if (auto *Def = dyn_cast<DefInit>(&I)) {
- obj["kind"] = "def";
- obj["def"] = Def->getDef()->getName();
- return std::move(obj);
- } else if (auto *Var = dyn_cast<VarInit>(&I)) {
- obj["kind"] = "var";
- obj["var"] = Var->getName();
- return std::move(obj);
- } else if (auto *VarBit = dyn_cast<VarBitInit>(&I)) {
- if (auto *Var = dyn_cast<VarInit>(VarBit->getBitVar())) {
- obj["kind"] = "varbit";
- obj["var"] = Var->getName();
- obj["index"] = VarBit->getBitNum();
- return std::move(obj);
+ json::Object Obj;
+ Obj["printable"] = I.getAsString();
+
+ if (const auto *Def = dyn_cast<DefInit>(&I)) {
+ Obj["kind"] = "def";
+ Obj["def"] = Def->getDef()->getName();
+ return std::move(Obj);
+ }
+ if (const auto *Var = dyn_cast<VarInit>(&I)) {
+ Obj["kind"] = "var";
+ Obj["var"] = Var->getName();
+ return std::move(Obj);
+ }
+ if (const auto *VarBit = dyn_cast<VarBitInit>(&I)) {
+ if (const auto *Var = dyn_cast<VarInit>(VarBit->getBitVar())) {
+ Obj["kind"] = "varbit";
+ Obj["var"] = Var->getName();
+ Obj["index"] = VarBit->getBitNum();
+ return std::move(Obj);
}
- } else if (auto *Dag = dyn_cast<DagInit>(&I)) {
- obj["kind"] = "dag";
- obj["operator"] = translateInit(*Dag->getOperator());
+ }
+ if (const auto *Dag = dyn_cast<DagInit>(&I)) {
+ Obj["kind"] = "dag";
+ Obj["operator"] = translateInit(*Dag->getOperator());
if (auto name = Dag->getName())
- obj["name"] = name->getAsUnquotedString();
- json::Array args;
- for (unsigned i = 0, limit = Dag->getNumArgs(); i < limit; ++i) {
- json::Array arg;
- arg.push_back(translateInit(*Dag->getArg(i)));
- if (auto argname = Dag->getArgName(i))
- arg.push_back(argname->getAsUnquotedString());
+ Obj["name"] = name->getAsUnquotedString();
+ json::Array Args;
+ for (unsigned Idx = 0, E = Dag->getNumArgs(); Idx < E; ++Idx) {
+ json::Array Arg;
+ Arg.push_back(translateInit(*Dag->getArg(Idx)));
+ if (const auto ArgName = Dag->getArgName(Idx))
+ Arg.push_back(ArgName->getAsUnquotedString());
else
- arg.push_back(nullptr);
- args.push_back(std::move(arg));
+ Arg.push_back(nullptr);
+ Args.push_back(std::move(Arg));
}
- obj["args"] = std::move(args);
- return std::move(obj);
+ Obj["args"] = std::move(Args);
+ return std::move(Obj);
}
// Final fallback: anything that gets past here is simply given a
// kind field of 'complex', and the only other field is the standard
// 'printable' representation.
-
assert(!I.isConcrete());
- obj["kind"] = "complex";
- return std::move(obj);
+ Obj["kind"] = "complex";
+ return std::move(Obj);
}
void JSONEmitter::run(raw_ostream &OS) {
- json::Object root;
+ json::Object Root;
- root["!tablegen_json_version"] = 1;
+ Root["!tablegen_json_version"] = 1;
// Prepare the arrays that will list the instances of every class.
// We mostly fill those in by iterating over the superclasses of
@@ -127,64 +127,59 @@ void JSONEmitter::run(raw_ostream &OS) {
// class with no instances at all, so we do a preliminary iteration
// over the classes, invoking std::map::operator[] to default-
// construct the array for each one.
- std::map<std::string, json::Array> instance_lists;
- for (const auto &C : Records.getClasses()) {
- const auto Name = C.second->getNameInitAsString();
- (void)instance_lists[Name];
- }
+ std::map<std::string, json::Array> InstanceLists;
+ for (const auto &[ClassName, ClassRec] : Records.getClasses())
+ InstanceLists.emplace(ClassRec->getNameInitAsString(), json::Array());
// Main iteration over the defs.
- for (const auto &D : Records.getDefs()) {
- const auto Name = D.second->getNameInitAsString();
- auto &Def = *D.second;
+ for (const auto &[DefName, Def] : Records.getDefs()) {
+ const std::string Name = Def->getNameInitAsString();
- json::Object obj;
- json::Array fields;
+ json::Object Obj;
+ json::Array Fields;
- for (const RecordVal &RV : Def.getValues()) {
- if (!Def.isTemplateArg(RV.getNameInit())) {
+ for (const RecordVal &RV : Def->getValues()) {
+ if (!Def->isTemplateArg(RV.getNameInit())) {
auto Name = RV.getNameInitAsString();
if (RV.isNonconcreteOK())
- fields.push_back(Name);
- obj[Name] = translateInit(*RV.getValue());
+ Fields.push_back(Name);
+ Obj[Name] = translateInit(*RV.getValue());
}
}
- obj["!fields"] = std::move(fields);
+ Obj["!fields"] = std::move(Fields);
- json::Array superclasses;
- for (const auto &SuperPair : Def.getSuperClasses())
- superclasses.push_back(SuperPair.first->getNameInitAsString());
- obj["!superclasses"] = std::move(superclasses);
+ json::Array SuperClasses;
+ // Add this def to the instance list for each of its superclasses.
+ for (const auto &[SuperClass, Loc] : Def->getSuperClasses()) {
+ std::string SuperName = SuperClass->getNameInitAsString();
+ SuperClasses.push_back(SuperName);
+ InstanceLists[SuperName].push_back(Name);
+ }
- obj["!name"] = Name;
- obj["!anonymous"] = Def.isAnonymous();
+ Obj["!superclasses"] = std::move(SuperClasses);
- json::Array locs;
- for (const SMLoc Loc : Def.getLoc())
- locs.push_back(SrcMgr.getFormattedLocationNoOffset(Loc));
- obj["!locs"] = std::move(locs);
+ Obj["!name"] = Name;
+ Obj["!anonymous"] = Def->isAnonymous();
- root[Name] = std::move(obj);
+ json::Array Locs;
+ for (const SMLoc Loc : Def->getLoc())
+ Locs.push_back(SrcMgr.getFormattedLocationNoOffset(Loc));
+ Obj["!locs"] = std::move(Locs);
- // Add this def to the instance list for each of its superclasses.
- for (const auto &SuperPair : Def.getSuperClasses()) {
- auto SuperName = SuperPair.first->getNameInitAsString();
- instance_lists[SuperName].push_back(Name);
- }
+ Root[Name] = std::move(Obj);
}
// Make a JSON object from the std::map of instance lists.
- json::Object instanceof;
- for (auto kv: instance_lists)
- instanceof[kv.first] = std::move(kv.second);
- root["!instanceof"] = std::move(instanceof);
+ json::Object InstanceOf;
+ for (auto &[ClassName, Instances] : InstanceLists)
+ InstanceOf[ClassName] = std::move(Instances);
+ Root["!instanceof"] = std::move(InstanceOf);
// Done. Write the output.
- OS << json::Value(std::move(root)) << "\n";
+ OS << json::Value(std::move(Root)) << "\n";
}
-namespace llvm {
-
-void EmitJSON(RecordKeeper &RK, raw_ostream &OS) { JSONEmitter(RK).run(OS); }
-} // end namespace llvm
+void llvm::EmitJSON(const RecordKeeper &RK, raw_ostream &OS) {
+ JSONEmitter(RK).run(OS);
+}